stop remark from decoding HTML entities

This commit is contained in:
Shawn Erquhart 2017-09-26 15:22:34 -04:00
parent d3c12db8ef
commit 9e0d7696ee
4 changed files with 86 additions and 1 deletions

View File

@ -0,0 +1,24 @@
import unified from 'unified';
import markdownToRemark from 'remark-parse';
import remarkAllowHtmlEntities from '../remarkAllowHtmlEntities';
const process = markdown => {
const mdast = unified().use(markdownToRemark).use(remarkAllowHtmlEntities).parse(markdown);
/**
* The MDAST will look like:
*
* { type: 'root', children: [
* { type: 'paragraph', children: [
* // results here
* ]}
* ]}
*/
return mdast.children[0].children[0].value;
};
describe('remarkAllowHtmlEntities', () => {
it('should not decode HTML entities', () => {
expect(process('<div>')).toEqual('<div>');
});
});

View File

@ -8,7 +8,7 @@ describe('slate', () => {
expect(process('a\n')).toEqual('a\n'); expect(process('a\n')).toEqual('a\n');
}); });
xit('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>\n');
}); });

View File

@ -18,6 +18,7 @@ import remarkImagesToText from './remarkImagesToText';
import remarkShortcodes from './remarkShortcodes'; import remarkShortcodes from './remarkShortcodes';
import remarkEscapeMarkdownEntities from './remarkEscapeMarkdownEntities'; import remarkEscapeMarkdownEntities from './remarkEscapeMarkdownEntities';
import remarkStripTrailingBreaks from './remarkStripTrailingBreaks'; import remarkStripTrailingBreaks from './remarkStripTrailingBreaks';
import remarkAllowHtmlEntities from './remarkAllowHtmlEntities';
import slateToRemark from './slateRemark'; import slateToRemark from './slateRemark';
import registry from '../../../../lib/registry'; import registry from '../../../../lib/registry';
@ -66,6 +67,7 @@ export const markdownToRemark = markdown => {
const parsed = unified() const parsed = unified()
.use(markdownToRemarkPlugin, { fences: true, commonmark: true }) .use(markdownToRemarkPlugin, { fences: true, commonmark: true })
.use(markdownToRemarkRemoveTokenizers, { inlineTokenizers: ['url'] }) .use(markdownToRemarkRemoveTokenizers, { inlineTokenizers: ['url'] })
.use(remarkAllowHtmlEntities)
.parse(markdown); .parse(markdown);
/** /**

View File

@ -0,0 +1,59 @@
export default function remarkAllowHtmlEntities() {
this.Parser.prototype.inlineTokenizers.text = text;
/**
* This is a port of the `remark-parse` text tokenizer, adapted to exclude
* HTML entity decoding.
*/
function text(eat, value, silent) {
var self = this;
var methods;
var tokenizers;
var index;
var length;
var subvalue;
var position;
var tokenizer;
var name;
var min;
var now;
/* istanbul ignore if - never used (yet) */
if (silent) {
return true;
}
methods = self.inlineMethods;
length = methods.length;
tokenizers = self.inlineTokenizers;
index = -1;
min = value.length;
while (++index < length) {
name = methods[index];
if (name === 'text' || !tokenizers[name]) {
continue;
}
tokenizer = tokenizers[name].locator;
if (!tokenizer) {
eat.file.fail('Missing locator: `' + name + '`');
}
position = tokenizer.call(self, value, 1);
if (position !== -1 && position < min) {
min = position;
}
}
subvalue = value.slice(0, min);
eat(subvalue)({
type: 'text',
value: subvalue,
});
}
};