Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
as-op committed Sep 19, 2024
1 parent 25f9b88 commit 4786fac
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/commonmark/commonmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
30 changes: 16 additions & 14 deletions src/commonmark/commonmarkdataprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,25 @@ export default class CommonMarkDataProcessor {
// Turndown is filtering out empty paragraphs <p></p>, so we need to fix that with <p><br></p>
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([
Expand Down Expand Up @@ -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 '';
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/commonmark/utils/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions tests/commonmark/lists.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 4786fac

Please sign in to comment.