From 4786facaf5d03a1868d27686cb5a1da7f04c9e27 Mon Sep 17 00:00:00 2001 From: as-op Date: Thu, 19 Sep 2024 17:39:23 +0200 Subject: [PATCH] cleanup --- src/commonmark/commonmark.js | 1 - src/commonmark/commonmarkdataprocessor.js | 30 ++++++++++++----------- src/commonmark/utils/preprocessor.js | 4 +-- tests/commonmark/lists.test.js | 4 --- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/commonmark/commonmark.js b/src/commonmark/commonmark.js index 8978d48..4c3d718 100644 --- a/src/commonmark/commonmark.js +++ b/src/commonmark/commonmark.js @@ -6,7 +6,6 @@ import CommonMarkDataProcessor from './commonmarkdataprocessor'; // Simple plugin which loads the data processor. -// eslint-disable-next-line no-unused-vars export default function CommonMarkPlugin(editor) { editor.data.processor = new CommonMarkDataProcessor(editor.editing.view.document); } diff --git a/src/commonmark/commonmarkdataprocessor.js b/src/commonmark/commonmarkdataprocessor.js index 0bd371f..6bbf889 100644 --- a/src/commonmark/commonmarkdataprocessor.js +++ b/src/commonmark/commonmarkdataprocessor.js @@ -106,23 +106,25 @@ export default class CommonMarkDataProcessor { // Turndown is filtering out empty paragraphs

, so we need to fix that with


breaksPreprocessor(domFragment); + const blankReplacement = function (content, node) { + if (node.tagName === 'CODE') { + // we don't want to remove code silently + const prefix = (node.getAttribute('class') || '').replace('language-', ''); + const textContent = node.textContent || ''; + + return "```" + prefix + '\n' + (textContent.length ? textContent : '\n') + "```\n"; + // we don't want to remove pre silently + } else if (node.tagName === 'PRE') { + return content; + } + return node.isBlock ? '\n\n' : '' + }; + // Use Turndown to convert DOM fragment to markdown const turndownService = new TurndownService({ headingStyle: 'atx', codeBlockStyle: 'fenced', - blankReplacement: function (content, node) { - if (node.tagName === 'CODE') { - // we don't want to remove code silently - const prefix = (node.getAttribute('class') || '').replace('language-', ''); - const textContent = node.textContent || ''; - - return "```" + prefix + '\n' + (textContent.length ? textContent : '\n') + "```\n"; - // we don't want to remove pre silently - } else if (node.tagName === 'PRE') { - return content; - } - return node.isBlock ? '\n\n' : '' - }, + blankReplacement: blankReplacement, }); turndownService.use([ @@ -170,7 +172,7 @@ export default class CommonMarkDataProcessor { // figure and the image in the imageFigure rule turndownService.addRule('figcaption', { filter: 'figcaption', - replacement: function (content, node) { + replacement: function (_content, _node) { return ''; } }); diff --git a/src/commonmark/utils/preprocessor.js b/src/commonmark/utils/preprocessor.js index a01127b..d85de4b 100644 --- a/src/commonmark/utils/preprocessor.js +++ b/src/commonmark/utils/preprocessor.js @@ -38,7 +38,7 @@ export function textNodesPreprocessor(root, allowed_whitespace_nodes, allowed_ra * @param {*} allowed_whitespace_nodes * @param {*} allowed_raw_nodes */ -export function linkPreprocessor(root, allowed_whitespace_nodes, allowed_raw_nodes) { +export function linkPreprocessor(root, _allowed_whitespace_nodes, _allowed_raw_nodes) { let walker = document.createNodeIterator( root, // Only consider element nodes @@ -57,7 +57,7 @@ export function linkPreprocessor(root, allowed_whitespace_nodes, allowed_raw_nod } } -export function breaksPreprocessor(root, allowed_whitespace_nodes, allowed_raw_nodes) { +export function breaksPreprocessor(root, _allowed_whitespace_nodes, _allowed_raw_nodes) { let walker = document.createNodeIterator( root, NodeFilter.SHOW_ELEMENT, diff --git a/tests/commonmark/lists.test.js b/tests/commonmark/lists.test.js index 30a8e03..188f411 100644 --- a/tests/commonmark/lists.test.js +++ b/tests/commonmark/lists.test.js @@ -4,10 +4,6 @@ */ import {testDataProcessor} from './_utils/utils.js'; -import MarkdownDataProcessor from '../../src/commonmark/commonmarkdataprocessor'; -import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor.js'; -import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document.js'; -import {StylesProcessor} from '@ckeditor/ckeditor5-engine/src/view/stylesmap.js'; describe('CommonMarkProcessor', () => { describe('lists', () => {