/** * @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 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'; /** * 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 ); } /** * 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() } ); 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 toMarkdown( html, { gfm: true, converters } ); } }