/
gfmdataprocessor.js
72 lines (65 loc) · 2.22 KB
/
gfmdataprocessor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module markdown-gfm/gfmdataprocessor
*/
import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
import markdown2html from './markdown2html/markdown2html';
import html2markdown, { turndownService } from './html2markdown/html2markdown';
/**
* This data processor implementation uses GitHub Flavored Markdown as input/output data.
*
* See the {@glink features/markdown Markdown output} guide to learn more on how to enable it.
*
* @implements module:engine/dataprocessor/dataprocessor~DataProcessor
*/
export default class GFMDataProcessor {
/**
* Creates a new instance of the Markdown data processor class.
*
* @param {module:engine/view/document~Document} document
*/
constructor( document ) {
/**
* HTML data processor used to process HTML produced by the Markdown-to-HTML converter and the other way.
*
* @private
* @member {module:engine/dataprocessor/htmldataprocessor~HtmlDataProcessor}
*/
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 = markdown2html( data );
return this._htmlDP.toView( html );
}
/**
* Converts the provided {@link module:engine/view/documentfragment~DocumentFragment} to data format — in this
* case to a Markdown string.
*
* @param {module:engine/view/documentfragment~DocumentFragment} viewFragment
* @returns {String} Markdown string.
*/
toData( viewFragment ) {
const html = this._htmlDP.toData( viewFragment );
return html2markdown( html );
}
}