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]);
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', () => {
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', () => {
expect(process('**a[b](c)d**')).toEqual('**a[b](c)d**\n');
expect(process('**[a](b)**')).toEqual('**[a](b)**\n');
expect(process('**![a](b)**')).toEqual('**![a](b)**\n');
expect(process('_`a`_')).toEqual('_`a`_\n');
expect(process('_`a`b_')).toEqual('_`a`b_\n');
expect(process('**a[b](c)d**')).toEqual('**a[b](c)d**');
expect(process('**[a](b)**')).toEqual('**[a](b)**');
expect(process('**![a](b)**')).toEqual('**![a](b)**');
expect(process('_`a`_')).toEqual('_`a`_');
expect(process('_`a`b_')).toEqual('_`a`b_');
});
it('should condense adjacent, identically styled text and inline nodes', () => {
expect(process('**a ~~b~~~~c~~**')).toEqual('**a ~~bc~~**\n');
expect(process('**a ~~b~~~~[c](d)~~**')).toEqual('**a ~~b[c](d)~~**\n');
expect(process('**a ~~b~~~~c~~**')).toEqual('**a ~~bc~~**');
expect(process('**a ~~b~~~~[c](d)~~**')).toEqual('**a ~~b[c](d)~~**');
});
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**\n');
expect(process('**a**b**c**')).toEqual('**a**b**c**');
expect(process('**a _b_ c**')).toEqual('**a _b_ c**');
});
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', () => {
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 u from 'unist-builder';
import markdownToRemarkPlugin from 'remark-parse';
@ -118,17 +118,19 @@ export const remarkToMarkdown = obj => {
fences: true,
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: '*',
strong: '*',
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(remarkStripTrailingBreaks)
.runSync(mdast);
@ -136,9 +138,12 @@ export const remarkToMarkdown = obj => {
const markdown = unified()
.use(remarkToMarkdownPlugin, remarkToMarkdownPluginOpts)
.use(remarkAllowAllText)
.stringify(escapedMdast);
.stringify(processedMdast);
return markdown;
/**
* Return markdown with trailing whitespace removed.
*/
return trimEnd(markdown);
};