trim trailing whitespace from markdown

This commit is contained in:
Shawn Erquhart 2017-10-04 12:53:28 -04:00 committed by Benaiah Mischenko
parent 852b6f397c
commit be4609e54d
2 changed files with 24 additions and 23 deletions

View File

@ -4,37 +4,33 @@ import { markdownToSlate, slateToMarkdown } from '../index';
const process = flow([markdownToSlate, slateToMarkdown]); const process = flow([markdownToSlate, slateToMarkdown]);
describe('slate', () => { describe('slate', () => {
it('should distinguish between newlines and hard breaks', () => {
expect(process('a\n')).toEqual('a\n');
});
it('should not decode encoded html entities in inline code', () => { it('should not decode encoded html entities in inline code', () => {
expect(process('<code>&lt;div&gt;</code>')).toEqual('<code>&lt;div&gt;</code>\n'); expect(process('<code>&lt;div&gt;</code>')).toEqual('<code>&lt;div&gt;</code>');
}); });
it('should parse non-text children of mark nodes', () => { it('should parse non-text children of mark nodes', () => {
expect(process('**a[b](c)d**')).toEqual('**a[b](c)d**\n'); expect(process('**a[b](c)d**')).toEqual('**a[b](c)d**');
expect(process('**[a](b)**')).toEqual('**[a](b)**\n'); expect(process('**[a](b)**')).toEqual('**[a](b)**');
expect(process('**![a](b)**')).toEqual('**![a](b)**\n'); expect(process('**![a](b)**')).toEqual('**![a](b)**');
expect(process('_`a`_')).toEqual('_`a`_\n'); expect(process('_`a`_')).toEqual('_`a`_');
expect(process('_`a`b_')).toEqual('_`a`b_\n'); expect(process('_`a`b_')).toEqual('_`a`b_');
}); });
it('should condense adjacent, identically styled text and inline nodes', () => { it('should condense adjacent, identically styled text and inline nodes', () => {
expect(process('**a ~~b~~~~c~~**')).toEqual('**a ~~bc~~**\n'); expect(process('**a ~~b~~~~c~~**')).toEqual('**a ~~bc~~**');
expect(process('**a ~~b~~~~[c](d)~~**')).toEqual('**a ~~b[c](d)~~**\n'); expect(process('**a ~~b~~~~[c](d)~~**')).toEqual('**a ~~b[c](d)~~**');
}); });
it('should handle nested markdown entities', () => { it('should handle nested markdown entities', () => {
expect(process('**a**b**c**')).toEqual('**a**b**c**\n'); expect(process('**a**b**c**')).toEqual('**a**b**c**');
expect(process('**a _b_ c**')).toEqual('**a _b_ c**\n'); expect(process('**a _b_ c**')).toEqual('**a _b_ c**');
}); });
it('should parse inline images as images', () => { it('should parse inline images as images', () => {
expect(process('a ![b](c)')).toEqual('a ![b](c)\n'); expect(process('a ![b](c)')).toEqual('a ![b](c)');
}); });
it('should not escape markdown entities in html', () => { it('should not escape markdown entities in html', () => {
expect(process('<span>*</span>')).toEqual('<span>*</span>\n'); expect(process('<span>*</span>')).toEqual('<span>*</span>');
}); });
}); });

View File

@ -1,4 +1,4 @@
import { get, isEmpty, reduce, pull } from 'lodash'; import { get, isEmpty, reduce, pull, trimEnd } from 'lodash';
import unified from 'unified'; import unified from 'unified';
import u from 'unist-builder'; import u from 'unist-builder';
import markdownToRemarkPlugin from 'remark-parse'; import markdownToRemarkPlugin from 'remark-parse';
@ -118,17 +118,19 @@ export const remarkToMarkdown = obj => {
fences: true, fences: true,
listItemIndent: '1', listItemIndent: '1',
// Settings to emulate the defaults from the Prosemirror editor, not /**
// necessarily optimal. Should eventually be configurable. * Settings to emulate the defaults from the Prosemirror editor, not
* necessarily optimal. Should eventually be configurable.
*/
bullet: '*', bullet: '*',
strong: '*', strong: '*',
rule: '-', rule: '-',
}; };
/** /**
* Escape markdown entities found in text and html nodes within the MDAST. * Transform the MDAST with plugins.
*/ */
const escapedMdast = unified() const processedMdast = unified()
.use(remarkEscapeMarkdownEntities) .use(remarkEscapeMarkdownEntities)
.use(remarkStripTrailingBreaks) .use(remarkStripTrailingBreaks)
.runSync(mdast); .runSync(mdast);
@ -136,9 +138,12 @@ export const remarkToMarkdown = obj => {
const markdown = unified() const markdown = unified()
.use(remarkToMarkdownPlugin, remarkToMarkdownPluginOpts) .use(remarkToMarkdownPlugin, remarkToMarkdownPluginOpts)
.use(remarkAllowAllText) .use(remarkAllowAllText)
.stringify(escapedMdast); .stringify(processedMdast);
return markdown; /**
* Return markdown with trailing whitespace removed.
*/
return trimEnd(markdown);
}; };