Skip to content

Commit

Permalink
trim trailing whitespace from markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
erquhart committed Oct 4, 2017
1 parent b8d5e4e commit 3fbe45e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
28 changes: 12 additions & 16 deletions src/components/Widgets/Markdown/serializers/__tests__/slate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>');
});
});
19 changes: 12 additions & 7 deletions src/components/Widgets/Markdown/serializers/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -118,27 +118,32 @@ 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);

const markdown = unified()
.use(remarkToMarkdownPlugin, remarkToMarkdownPluginOpts)
.use(remarkAllowAllText)
.stringify(escapedMdast);
.stringify(processedMdast);

return markdown;
/**
* Return markdown with trailing whitespace removed.
*/
return trimEnd(markdown);
};


Expand Down

0 comments on commit 3fbe45e

Please sign in to comment.