UNPKG

ckeditor5-image-upload-base64

Version:

The development environment of CKEditor 5 – the best browser-based rich text editor.

73 lines (65 loc) 2.27 kB
/** * @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 ); } }