Browse Source

Added tests for toData method.

Szymon Kupś 9 years ago
parent
commit
56cfdeaa84

+ 5 - 4
packages/ckeditor5-markdown-gfm/src/gfmdataprocessor.js

@@ -3,10 +3,11 @@
  * For licensing, see LICENSE.md.
  */
 
-import marked from './lib/marked.js';
-import toMarkdown from './lib/to-markdown.js';
+import marked from './lib/marked/marked.js';
+import toMarkdown from './lib/to-markdown/to-markdown.js';
 import HtmlDataProcessor from '../engine/dataprocessor/htmldataprocessor.js';
-import GFMRenderer from './renderer.js';
+import GFMRenderer from './lib/marked/renderer.js';
+import converters from './lib/to-markdown/converters.js';
 
 export default class GFMDataProcessor extends HtmlDataProcessor {
 	constructor() {
@@ -28,7 +29,7 @@ export default class GFMDataProcessor extends HtmlDataProcessor {
 	toData( viewFragment ) {
 		const html = super.toData( viewFragment );
 
-		return toMarkdown( html, { gfm: true } );
+		return toMarkdown( html, { gfm: true, converters } );
 	}
 }
 

packages/ckeditor5-markdown-gfm/src/lib/marked.js → packages/ckeditor5-markdown-gfm/src/lib/marked/marked.js


packages/ckeditor5-markdown-gfm/src/renderer.js → packages/ckeditor5-markdown-gfm/src/lib/marked/renderer.js


+ 26 - 0
packages/ckeditor5-markdown-gfm/src/lib/to-markdown/converters.js

@@ -0,0 +1,26 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+// Exports an array with custom converters used by to-markdown library.
+export default [
+
+	// Converting code blocks with class name matching output from marked library.
+	{
+		filter: (node) =>  {
+			const regexp = /lang-(.+)/;
+
+			return node.nodeName === 'PRE' &&
+				node.firstChild &&
+				node.firstChild.nodeName === 'CODE' &&
+				regexp.test( node.firstChild.className );
+		},
+		replacement: ( content, node ) => {
+			const regexp = /lang-(.+)/;
+			const lang = regexp.exec( node.firstChild.className )[ 1 ];
+
+			return '\n\n``` ' + lang + '\n' + node.firstChild.textContent + '\n```\n\n';
+		}
+	},
+];

packages/ckeditor5-markdown-gfm/src/lib/to-markdown.js → packages/ckeditor5-markdown-gfm/src/lib/to-markdown/to-markdown.js


+ 84 - 1
packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/blockquotes.js

@@ -4,7 +4,7 @@
  */
 
 import MarkdownDataProcessor from '/ckeditor5/markdown-gfm/gfmdataprocessor.js';
-import { stringify } from '/tests/engine/_utils/view.js';
+import { stringify, parse } from '/tests/engine/_utils/view.js';
 
 describe( 'GFMDataProcessor', () => {
 	let dataProcessor;
@@ -85,5 +85,88 @@ describe( 'GFMDataProcessor', () => {
 				);
 			} );
 		} );
+
+		describe( 'toData', () => {
+			it( 'should process single blockquotes', () => {
+				const viewFragment = parse( '<blockquote><p>foo bar</p></blockquote>' );
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal( '> foo bar' );
+			} );
+
+			it( 'should process nested blockquotes', () => {
+				const viewFragment = parse(
+					'<blockquote>' +
+						'<p>foo</p>' +
+						'<blockquote>' +
+							'<p>bar</p>' +
+						'</blockquote>' +
+						'<p>foo</p>' +
+					'</blockquote>'
+				);
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal(
+					'> foo\n' +
+					'> \n' +
+					'> > bar\n' +
+					'> \n' +
+					'> foo'
+				);
+			} );
+
+			it( 'should process list within a blockquote', () => {
+				const viewFragment = parse(
+					'<blockquote>' +
+						'<p>A list within a blockquote:</p>' +
+						'<ul>' +
+							'<li>asterisk 1</li>' +
+							'<li>asterisk 2</li>' +
+							'<li>asterisk 3</li>' +
+						'</ul>' +
+					'</blockquote>'
+				);
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal(
+					'> A list within a blockquote:\n' +
+					'> \n' +
+					'> *   asterisk 1\n' +
+					'> *   asterisk 2\n' +
+					'> *   asterisk 3'
+				);
+			} );
+
+			it( 'should process blockquotes with code inside', () => {
+				const viewFragment = parse(
+					'<blockquote>' +
+						'<p>Example 1:</p>' +
+						'<pre>' +
+							'<code>' +
+								'code' +
+							'</code>' +
+						'</pre>' +
+						'<p>Example 2:</p>' +
+						'<pre>' +
+							'<code>' +
+								'code' +
+							'</code>' +
+						'</pre>' +
+					'</blockquote>',
+					{ sameSelectionCharacters: true }
+				);
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal(
+					'> Example 1:\n' +
+					'> \n' +
+					'> ```\n' +
+					'> code\n' +
+					'> ```\n' +
+					'> \n' +
+					'> Example 2:\n' +
+					'> \n' +
+					'> ```\n' +
+					'> code\n' +
+					'> ```'
+				);
+			} );
+		} );
 	} );
 } );

+ 25 - 1
packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/code.js

@@ -4,7 +4,7 @@
  */
 
 import MarkdownDataProcessor from '/ckeditor5/markdown-gfm/gfmdataprocessor.js';
-import { stringify } from '/tests/engine/_utils/view.js';
+import { stringify, parse } from '/tests/engine/_utils/view.js';
 
 describe( 'GFMDataProcessor', () => {
 	let dataProcessor;
@@ -169,5 +169,29 @@ describe( 'GFMDataProcessor', () => {
 				);
 			} );
 		} );
+
+		describe( 'toData', () => {
+			it( 'should process inline code', () => {
+				const viewFragment = parse( '<p>regular text and <code>inline code</code></p>' );
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal( 'regular text and `inline code`' );
+			} );
+
+			it( 'should properly process code blocks', () => {
+				const viewFragment = parse( '<pre><code>code block</code></pre>' );
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal( '```\ncode block\n```' );
+			} );
+
+			it( 'should process code block with language name', () => {
+				const viewFragment = parse( '<pre><code class="lang-js">code block</code></pre>' );
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal(
+					'``` js\n' +
+					'code block\n' +
+					'```'
+				);
+			} );
+		} );
 	} );
 } );

+ 15 - 1
packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/del.js

@@ -4,7 +4,7 @@
  */
 
 import MarkdownDataProcessor from '/ckeditor5/markdown-gfm/gfmdataprocessor.js';
-import { stringify } from '/tests/engine/_utils/view.js';
+import { stringify, parse } from '/tests/engine/_utils/view.js';
 
 describe( 'GFMDataProcessor', () => {
 	let dataProcessor;
@@ -27,5 +27,19 @@ describe( 'GFMDataProcessor', () => {
 				expect( stringify( viewFragment ) ).to.equal( '<p>This is <del>deleted content</del>.</p>' );
 			} );
 		} );
+
+		describe( 'toData', () => {
+			it( 'should process deleted text', () => {
+				const viewFragment = parse( '<p><del>deleted</del></p>' );
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal( '~~deleted~~' );
+			} );
+
+			it( 'should process deleted inside text', () => {
+				const viewFragment = parse( '<p>This is <del>deleted content</del>.</p>' );
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal( 'This is ~~deleted content~~.' );
+			} );
+		} );
 	} );
 } );

+ 27 - 1
packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/strong_and_emphasis.js

@@ -4,7 +4,7 @@
  */
 
 import MarkdownDataProcessor from '/ckeditor5/markdown-gfm/gfmdataprocessor.js';
-import { stringify } from '/tests/engine/_utils/view.js';
+import { stringify, parse } from '/tests/engine/_utils/view.js';
 
 describe( 'GFMDataProcessor', () => {
 	let dataProcessor;
@@ -69,5 +69,31 @@ describe( 'GFMDataProcessor', () => {
 				expect( stringify( viewFragment ) ).to.equal( '<p><em>test <strong>test</strong> test</em></p>' );
 			} );
 		} );
+
+		describe( 'toData', () => {
+			it( 'should process strong', () => {
+				const viewFragment = parse( '<p><strong>strong</strong> and <strong>this too</strong></p>' );
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal( '**strong** and **this too**' );
+			} );
+
+			it( 'should process emphasis', () => {
+				const viewFragment = parse( '<p><em>emphasis</em> and <em>this too</em></p>' );
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal( '_emphasis_ and _this too_' );
+			} );
+
+			it( 'should process strong and emphasis together #1', () => {
+				const viewFragment = parse( '<p><strong><em>This is strong and em.</em></strong></p>' );
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal( '**_This is strong and em._**' );
+			} );
+
+			it( 'should process strong and emphasis together #2', () => {
+				const viewFragment = parse( '<p><em><strong>This is strong and em.</strong></em></p>' );
+
+				expect( dataProcessor.toData( viewFragment ) ).to.equal( '_**This is strong and em.**_' );
+			} );
+		} );
 	} );
 } );