Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete revamp of markdown-gfm #7307

Merged
merged 32 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f537b9a
Migrated from to-markdown to Turndown (except converters).
fredck May 26, 2020
ecd001f
Importing 'marked' as an npm module and separate it into it's own code.
fredck May 26, 2020
9c55502
Fixed the conversion of todo lists to match the default HTML generate…
fredck May 26, 2020
1612929
Configured markdown to output fenced (```) code blocks.
fredck May 26, 2020
18dc7cc
Minor clarification in a code comment.
fredck May 26, 2020
a05667f
Changed the defaults of some of the markdown markup produced.
fredck May 26, 2020
8b63c53
Aligned some of the tests with the current expected behavior of featu…
fredck May 26, 2020
cd4b9f9
Changed test that are outdated, low quality or irrelevant.
fredck May 26, 2020
e26e376
Removed outdated code, confirmed to not be anymore needed.
fredck May 26, 2020
52aed7f
HTML as text is now properly escaped in the output mardown.
fredck May 26, 2020
e593849
Added a basic test for todo lists.
fredck May 26, 2020
c30bd15
Added a test for todo lists when loading the HTML produced by the tod…
fredck May 26, 2020
3cb85fa
Fixed an unfortunate typo in the todo list tests.
fredck May 26, 2020
6eb57fc
For the purpose of this plugin, autolinking from markdown to html is …
fredck May 26, 2020
129da7d
Upgrade to v18.0.0.
fredck May 26, 2020
43cce7f
Made it possible to define html elements to be kept as is in the output.
fredck May 26, 2020
5a76d6a
Tricked JSDoc on not failing validation.
fredck May 27, 2020
a0b1ad1
Fixed linting issues.
fredck Jun 25, 2020
a32884d
Added test for code blocks with triple/quadruple ticks inside.
fredck May 26, 2020
390a3cf
Upgraded to Turndown v6.0.0.
fredck May 26, 2020
ae368f4
Fixed the html->markdown conversion to avoid escaping urls in text no…
fredck May 26, 2020
b3e93b4
Merge branch 'i/github-writer/17' into i/5988
fredck Aug 11, 2020
db22d92
Merge branch 'i/github-writer/93' into i/5988
fredck Aug 11, 2020
0e2f99e
Merge branch 'master' into i/5988
jodator Aug 13, 2020
2c10447
Update yarn.lock.
jodator Aug 13, 2020
36c0f9c
Match code style to the rest of the modules.
jodator Aug 14, 2020
85d2030
Add test for escaping text between 2 URLs.
jodator Aug 14, 2020
49d86f1
Use String.matchAll() instead of do-while for regex matches traversal.
jodator Aug 14, 2020
317fcfa
Update markdown gfm module documentation.
jodator Aug 17, 2020
00de1de
Update markdown-gfm manual test.
jodator Aug 17, 2020
516ed39
Fix condition in html2markdown processing.
jodator Aug 17, 2020
d7dbe8c
Merge branch 'master' into i/5988
jodator Aug 17, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/ckeditor5-markdown-gfm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"ckeditor5-plugin"
],
"dependencies": {
"@ckeditor/ckeditor5-engine": "^21.0.0"
"@ckeditor/ckeditor5-engine": "^21.0.0",
"marked": "^0.7.0",
"turndown": "^6.0.0",
"turndown-plugin-gfm": "^1.0.2"
},
"engines": {
"node": ">=12.0.0",
Expand Down
31 changes: 17 additions & 14 deletions packages/ckeditor5-markdown-gfm/src/gfmdataprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
* @module markdown-gfm/gfmdataprocessor
*/

import marked from './lib/marked/marked';
import toMarkdown from './lib/to-markdown/to-markdown';
import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
import GFMRenderer from './lib/marked/renderer';
import converters from './lib/to-markdown/converters';

import markdown2html from './markdown2html/markdown2html';
import html2markdown, { turndownService } from './html2markdown/html2markdown';

/**
* This data processor implementation uses GitHub Flavored Markdown as input/output data.
Expand All @@ -36,21 +35,26 @@ export default class GFMDataProcessor {
this._htmlDP = new HtmlDataProcessor( document );
}

/**
* Keeps the specified element in the output as HTML. This is useful if the editor contains
* features that produce HTML that are not part of the markdon standards.
*
* By default, all HTML tags are removed.
*
* @param element {String} The element name to be kept.
*/
keepHtml( element ) {
turndownService.keep( [ element ] );
}

/**
* Converts the provided Markdown string to view tree.
*
* @param {String} data A Markdown string.
* @returns {module:engine/view/documentfragment~DocumentFragment} The converted view element.
*/
toView( data ) {
const html = marked.parse( data, {
gfm: true,
breaks: true,
tables: true,
xhtml: true,
renderer: new GFMRenderer()
} );

const html = markdown2html( data );
return this._htmlDP.toView( html );
}

Expand All @@ -63,7 +67,6 @@ export default class GFMDataProcessor {
*/
toData( viewFragment ) {
const html = this._htmlDP.toData( viewFragment );

return toMarkdown( html, { gfm: true, converters } );
return html2markdown( html );
}
}
93 changes: 93 additions & 0 deletions packages/ckeditor5-markdown-gfm/src/html2markdown/html2markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

/**
* @module markdown-gfm/html2markdown
*/

import TurndownService from 'turndown';
import { gfm } from 'turndown-plugin-gfm';

// Override the original escape method by not escaping links.
const originalEscape = TurndownService.prototype.escape;

function escape( string ) {
string = originalEscape( string );

// Escape "<".
string = string.replace( /</g, '\\<' );

return string;
}

// eslint-disable-next-line max-len
const regex = /\b(?:https?:\/\/|www\.)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()[\]{};:'".,<>?«»“”‘’])/g;

TurndownService.prototype.escape = function( string ) {
// Urls should not be escaped. Our strategy is using a regex to find them and escape everything
// which is out of the matches parts.

let escaped = '';
let lastLinkEnd = 0;

for ( const match of string.matchAll( regex ) ) {
const index = match.index;

// Append the substring between the last match and the current one (if anything).
if ( index > lastLinkEnd ) {
escaped += escape( string.substring( lastLinkEnd, index ) );
}

const matchedURL = match[ 0 ];

escaped += matchedURL;

lastLinkEnd = index + matchedURL.length;
}

// Add text after the last link or at the string start if no matches.
if ( lastLinkEnd < string.length ) {
escaped += escape( string.substring( lastLinkEnd, string.length ) );
}

return escaped;
};

const turndownService = new TurndownService( {
codeBlockStyle: 'fenced',
hr: '---',
headingStyle: 'atx'
} );

turndownService.use( [
gfm,
todoList
] );

/**
* Parses HTML to a markdown.
*
* @param {String} html
* @returns {String}
*/
export default function html2markdown( html ) {
return turndownService.turndown( html );
}

export { turndownService };

// This is a copy of the original taskListItems rule from turdown-plugin-gfm, with minor changes.
function todoList( turndownService ) {
turndownService.addRule( 'taskListItems', {
filter( node ) {
return node.type === 'checkbox' &&
// Changes here as CKEditor outputs a deeper structure.
( node.parentNode.nodeName === 'LI' || node.parentNode.parentNode.nodeName === 'LI' );
},
replacement( content, node ) {
return ( node.checked ? '[x]' : '[ ]' ) + ' ';
}
} );
}
Loading