瀏覽代碼

Made it possible to define html elements to be kept as is in the output.

fredck 5 年之前
父節點
當前提交
43cce7f703

+ 13 - 1
packages/ckeditor5-markdown-gfm/src/gfmdataprocessor.js

@@ -10,7 +10,7 @@
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 
 import markdown2html from './markdown2html/markdown2html';
-import html2markdown from './html2markdown/html2markdown';
+import html2markdown, { turndownService } from './html2markdown/html2markdown';
 
 /**
  * This data processor implementation uses GitHub Flavored Markdown as input/output data.
@@ -35,6 +35,18 @@ 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.
 	 *

+ 2 - 0
packages/ckeditor5-markdown-gfm/src/html2markdown/html2markdown.js

@@ -34,6 +34,8 @@ 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', {

+ 2 - 0
packages/ckeditor5-markdown-gfm/src/markdown2html/markdown2html.js

@@ -15,6 +15,8 @@ export default function markdown2html( markdown ) {
 	} );
 }
 
+export { marked };
+
 // Overrides.
 
 // Disable the autolink rule in the lexer (point it to a regex that always fail).

+ 6 - 1
packages/ckeditor5-markdown-gfm/tests/_utils/utils.js

@@ -14,11 +14,16 @@ import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
  * @param {String} markdown Markdown to be processed to view.
  * @param {String} viewString Expected view structure.
  * @param {String} [normalizedMarkdown] When converting back to the markdown it might be different than provided input
+ * @param {Object} [options] Additional options.
+ * @param {Function} [options.setup] A function that receives the data processor instance before its execution.
  * markdown string (which will be used if this parameter is not provided).
  */
-export function testDataProcessor( markdown, viewString, normalizedMarkdown ) {
+export function testDataProcessor( markdown, viewString, normalizedMarkdown, options ) {
 	const viewDocument = new ViewDocument( new StylesProcessor() );
+
 	const dataProcessor = new MarkdownDataProcessor( viewDocument );
+	options && options.setup && options.setup( dataProcessor );
+
 	const viewFragment = dataProcessor.toView( markdown );
 
 	const html = cleanHtml( stringify( viewFragment ) );

+ 26 - 0
packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/html.js

@@ -0,0 +1,26 @@
+/**
+ * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+import { testDataProcessor } from '../_utils/utils';
+
+describe( 'GFMDataProcessor', () => {
+	describe( 'html', () => {
+		it( 'should keep html', () => {
+			testDataProcessor(
+				'test with <keep>html</keep> and <notkeep>not html</notkeep>',
+
+				'<p>test with <keep>html</keep> and <notkeep>not html</notkeep></p>',
+
+				'test with <keep>html</keep> and not html',
+
+				{
+					setup: dataProcessor => {
+						dataProcessor.keepHtml( 'keep' );
+					}
+				}
+			);
+		} );
+	} );
+} );