Browse Source

Added initial implementation of GFMDataProcessor and some tests.

Szymon Kupś 9 years ago
parent
commit
9fbf60d4b4

+ 31 - 0
packages/ckeditor5-markdown-gfm/src/gfmdataprocessor.js

@@ -0,0 +1,31 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import marked from './lib/marked.js';
+import toMarkdown from './lib/to-markdown.js';
+import HtmlDataProcessor from '../engine/dataprocessor/htmldataprocessor.js';
+
+export default class GFMDataProcessor extends HtmlDataProcessor {
+	constructor() {
+		super();
+	}
+
+	toView( data ) {
+		const html = marked.parse( data, {
+			gfm: true,
+			breaks: true,
+			xhtml: true
+		} );
+
+		return super.toView( html.replace( /\n/g, '' ) );
+	}
+
+	toData( viewFragment ) {
+		const html = super.toData( viewFragment );
+
+		return toMarkdown( html, { gfm: true } );
+	}
+}
+

File diff suppressed because it is too large
+ 1286 - 0
packages/ckeditor5-markdown-gfm/src/lib/marked.js


File diff suppressed because it is too large
+ 785 - 0
packages/ckeditor5-markdown-gfm/src/lib/to-markdown.js


+ 12 - 0
packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/_fixtures/blockquotes.js

@@ -0,0 +1,12 @@
+export default [
+	{
+		name: 'single',
+		md: '> foo bar',
+		html: '<blockquote><p>foo bar</p></blockquote>'
+	},
+	{
+		name: 'nested',
+		md: '> foo\n>\n> > bar\n>\n> foo\n',
+		html: '<blockquote><p>foo</p><blockquote><p>bar</p></blockquote><p>foo</p></blockquote>'
+	}
+];

+ 22 - 0
packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/_fixtures/code_blocks.js

@@ -0,0 +1,22 @@
+export default [
+	{
+		name: 'indented with tabs',
+		md: '	code block',
+		html: '<pre><code>code block</code></pre>'
+	},
+	{
+		name: 'indented with spaces',
+		md: '    code block',
+		html: '<pre><code>code block</code></pre>'
+	}
+	// {
+	// 	name: 'multiline',
+	// 	md: '	first line\n	second line',
+	// 	html: ''
+	// },
+	// {
+	// 	name: 'multiline with trailing spaces',
+	// 	md: '	the lines in this block  \n	all contain trailing spaces  ',
+	// 	html: '<pre><code>the lines in this blockall contain trailing spaces  </code></pre>'
+	// }
+];

+ 77 - 0
packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/_fixtures/lists.js

@@ -0,0 +1,77 @@
+export default [
+	{
+		name: 'tight asterisks',
+		md: '*	item 1\n*	item 2\n*	item 3',
+		html: '<ul><li>item 1</li><li>item 2</li><li>item 3</li></ul>'
+	},
+	{
+		name: 'loose asterisks',
+		md: '*	item 1\n\n*	item 2\n\n*	item 3',
+		html: '<ul><li><p>item 1</p></li><li><p>item 2</p></li><li><p>item 3</p></li></ul>'
+	},
+	{
+		name: 'tight pluses',
+		md: '+	item 1\n+	item 2\n+	item 3',
+		html: '<ul><li>item 1</li><li>item 2</li><li>item 3</li></ul>'
+	},
+	{
+		name: 'loose pluses',
+		md: '+	item 1\n\n+	item 2\n\n+	item 3',
+		html: '<ul><li><p>item 1</p></li><li><p>item 2</p></li><li><p>item 3</p></li></ul>'
+	},
+	{
+		name: 'tight minuses',
+		md: '-	item 1\n-	item 2\n-	item 3',
+		html: '<ul><li>item 1</li><li>item 2</li><li>item 3</li></ul>'
+	},
+	{
+		name: 'loose minuses',
+		md: '-	item 1\n\n-	item 2\n\n-	item 3',
+		html: '<ul><li><p>item 1</p></li><li><p>item 2</p></li><li><p>item 3</p></li></ul>'
+	},
+	{
+		name: 'ordered list with tabs',
+		md: '1.	item 1\n2.	item 2\n3.	item 3',
+		html: '<ol><li>item 1</li><li>item 2</li><li>item 3</li></ol>'
+	},
+	{
+		name: 'ordered list with spaces',
+		md: '1. item 1\n2. item 2\n3. item 3',
+		html: '<ol><li>item 1</li><li>item 2</li><li>item 3</li></ol>'
+	},
+	{
+		name: 'loose ordered list with tabs',
+		md: '1.	item 1\n\n2.	item 2\n\n3.	item 3',
+		html: '<ol><li><p>item 1</p></li><li><p>item 2</p></li><li><p>item 3</p></li></ol>'
+	},
+	{
+		name: 'loose ordered list with spaces',
+		md: '1. item 1\n\n2. item 2\n\n3. item 3',
+		html: '<ol><li><p>item 1</p></li><li><p>item 2</p></li><li><p>item 3</p></li></ol>'
+	},
+	{
+		name: 'multiple paragraphs',
+		md: '1.	Item 1, graf one.\n\n	Item 2. graf two. The quick brown fox jumped over the lazy dogs\n	back.\n	\n2.	Item 2.\n\n3.	Item 3.',
+		html: '<ol><li><p>Item 1, graf one.</p><p>Item 2. graf two. The quick brown fox jumped over the lazy dogs<br></br>back.</p></li><li><p>Item 2.</p></li><li><p>Item 3.</p></li></ol>'
+	},
+	{
+		name: 'nested',
+		md: '*	Tab\n	*	Tab\n		*	Tab',
+		html: '<ul><li>Tab<ul><li>Tab<ul><li>Tab</li></ul></li></ul></li></ul>'
+	},
+	{
+		name: 'nested and mixed',
+		md: '1. First\n2. Second:\n	* Fee\n	* Fie\n	* Foe\n3. Third',
+		html: '<ol><li>First</li><li>Second:<ul><li>Fee</li><li>Fie</li><li>Foe</li></ul></li><li>Third</li></ol>'
+	},
+	{
+		name: 'nested and mixed loose',
+		md: '1. First\n\n2. Second:\n	* Fee\n	* Fie\n	* Foe\n\n3. Third',
+		html: '<ol><li><p>First</p></li><li><p>Second:</p><ul><li>Fee</li><li>Fie</li><li>Foe</li></ul></li><li><p>Third</p></li></ol>'
+	},
+	{
+		name: 'edge case - error in Markdown 1.0.1',
+		md: '*	this\n\n	*	sub\n\n	that',
+		html: '<ul><li><p>this</p><ul><li>sub</li></ul><p>that</p></li></ul>'
+	}
+];

+ 32 - 0
packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/_fixtures/strong_and_emphasis.js

@@ -0,0 +1,32 @@
+export default [
+	{
+		name: 'should use strong',
+		md: '**this is strong** and __this too__',
+		html: '<p><strong>this is strong</strong> and <strong>this too</strong></p>'
+	},
+	{
+		name: 'should use emphasis',
+		md: '*this is emphasis* and _this too_',
+		html: '<p><em>this is emphasis</em> and <em>this too</em></p>'
+	},
+	{
+		name: 'should use strong and emphasis together #1',
+		md: '***This is strong and em.***',
+		html: '<p><strong><em>This is strong and em.</em></strong></p>'
+	},
+	{
+		name: 'should use strong and emphasis together #2',
+		md: 'Single ***word*** is strong and em.',
+		html: '<p>Single <strong><em>word</em></strong> is strong and em.</p>'
+	},
+	{
+		name: 'should use strong and emphasis together #2',
+		md: '___This is strong and em.___',
+		html: '<p><strong><em>This is strong and em.</em></strong></p>'
+	},
+	{
+		name: 'should use strong and emphasis together #2',
+		md: 'Single ___word___ is strong and em.',
+		html: '<p>Single <strong><em>word</em></strong> is strong and em.</p>'
+	}
+];

+ 17 - 0
packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/_fixtures/tabs.js

@@ -0,0 +1,17 @@
+export default [
+	{
+		name: 'should create list item when using tabs',
+		md: '+	this is a list item indented with tabs\n',
+		html: '<ul><li>this is a list item indented with tabs</li></ul>'
+	},
+	{
+		name: 'should create list item when using spaces',
+		md: '+   this is a list item indented with spaces\n\n',
+		html: '<ul><li>this is a list item indented with spaces</li></ul>'
+	},
+	{
+		name: 'should create code block indented by tab',
+		md: '	this code block is indented by one tab',
+		html: '<pre><code>this code block is indented by one tab</code></pre>'
+	}
+];

+ 7 - 0
packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/_fixtures/tidyness.js

@@ -0,0 +1,7 @@
+export default [
+	{
+		name: 'should create tidy output',
+		md: '> A list within a blockquote:\n> \n> *	asterisk 1\n> *	asterisk 2\n> *	asterisk 3\n',
+		html: '<blockquote><p>A list within a blockquote:</p><ul><li>asterisk 1</li><li>asterisk 2</li><li>asterisk 3</li></ul></blockquote>'
+	}
+];

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

@@ -0,0 +1,48 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import MarkdownDataProcessor from '/ckeditor5/markdown-gfm/gfmdataprocessor.js';
+import { stringify } from '/tests/engine/_utils/view.js';
+
+// Fixtures
+import tidynes from '/tests/markdown-gfm/gfmdataprocessor/_fixtures/tidyness.js';
+import tabs from '/tests/markdown-gfm/gfmdataprocessor/_fixtures/tabs.js';
+import strongAndEmphasis from '/tests/markdown-gfm/gfmdataprocessor/_fixtures/strong_and_emphasis.js';
+import lists from '/tests/markdown-gfm/gfmdataprocessor/_fixtures/lists.js';
+import blockquotes from '/tests/markdown-gfm/gfmdataprocessor/_fixtures/blockquotes.js';
+import codeBlocks from '/tests/markdown-gfm/gfmdataprocessor/_fixtures/code_blocks.js';
+
+const fixturesSets = {
+	'strong and emphasis': strongAndEmphasis,
+	'lists': lists,
+	'block quotes': blockquotes,
+	'code blocks': codeBlocks,
+	'tabs': tabs,
+	'tidyness': tidynes
+};
+
+describe( 'GFMDataProcessor', () => {
+	let dataProcessor;
+
+	beforeEach( () => {
+		dataProcessor = new MarkdownDataProcessor();
+	} );
+
+	describe( 'toView', () => {
+		for ( let fixtureSetName in fixturesSets ) {
+			const fixtureSet = fixturesSets[ fixtureSetName ];
+
+			describe( fixtureSetName, () => {
+				for ( let fixture of fixtureSet ) {
+					it( fixture.name, () => {
+						const viewFragment = dataProcessor.toView( fixture.md );
+
+						expect( stringify( viewFragment ) ).to.equal( fixture.html );
+					} );
+				}
+			} );
+		}
+	} );
+} );