Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow disabling tokenizers #119

Merged
merged 10 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions __tests__/__snapshots__/disabling-tokenizers.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`disabling block tokenizer 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "# heading 1",
},
],
"type": "paragraph",
},
],
"type": "root",
}
`;

exports[`disabling delete 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "~~strikethrough~~",
},
],
"type": "paragraph",
},
],
"type": "root",
}
`;

exports[`disabling emphasis 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "*emphatic **strong** emphatic*",
},
],
"type": "paragraph",
},
],
"type": "root",
}
`;

exports[`disabling inlineCode 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "\`const js = true \`",
},
],
"type": "paragraph",
},
],
"type": "root",
}
`;
115 changes: 115 additions & 0 deletions __tests__/__snapshots__/link-parsers.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`a bare autoLinked url 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "http://www.google.com",
},
],
"title": null,
"type": "link",
"url": "http://www.google.com",
},
],
"type": "paragraph",
},
],
"type": "root",
}
`;

exports[`a bare autoLinked url with no protocol 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "www.google.com",
},
],
"type": "paragraph",
},
],
"type": "root",
}
`;

exports[`a bracketed autoLinked url 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "http://www.google.com",
},
],
"title": null,
"type": "link",
"url": "http://www.google.com",
},
],
"type": "paragraph",
},
],
"type": "root",
}
`;

exports[`a link ref 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "link",
},
],
"identifier": "link",
"label": "link",
"referenceType": "shortcut",
"type": "linkReference",
},
],
"type": "paragraph",
},
],
"type": "root",
}
`;

exports[`a link with label 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "link",
},
],
"title": null,
"type": "link",
"url": "http://www.foo.com",
},
],
"type": "paragraph",
},
],
"type": "root",
}
`;
29 changes: 29 additions & 0 deletions __tests__/disabling-tokenizers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const markdown = require('../index');

test('disabling inlineCode', () => {
const md = '`const js = true `';
const opts = { disableTokenizers: { inline: ['code'] } };

expect(markdown.mdast(md, opts)).toMatchSnapshot();
});

test('disabling emphasis', () => {
const md = '*emphatic **strong** emphatic*';
const opts = { disableTokenizers: { inline: ['emphasis', 'strong'] } };

expect(markdown.mdast(md, opts)).toMatchSnapshot();
});

test('disabling delete', () => {
const md = '~~strikethrough~~';
const opts = { disableTokenizers: { inline: ['deletion'] } };

expect(markdown.mdast(md, opts)).toMatchSnapshot();
});

test('disabling block tokenizer', () => {
const md = '# heading 1';
const opts = { disableTokenizers: { block: ['atxHeading'] } };

expect(markdown.mdast(md, opts)).toMatchSnapshot();
});
21 changes: 21 additions & 0 deletions __tests__/link-parsers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const markdown = require('../index');

test('a link with label', () => {
expect(markdown.mdast('[link](http://www.foo.com)')).toMatchSnapshot();
});

test('a link ref', () => {
expect(markdown.mdast('[link]')).toMatchSnapshot();
});

test('a bracketed autoLinked url', () => {
expect(markdown.mdast('<http://www.google.com>')).toMatchSnapshot();
});

test('a bare autoLinked url', () => {
expect(markdown.mdast('http://www.googl.com')).toMatchSnapshot();
});

test('a bare autoLinked url with no protocol', () => {
expect(markdown.mdast('www.google.com')).toMatchSnapshot();
});
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const remarkStringify = require('remark-stringify');
const remarkBreaks = require('remark-breaks');
const remarkSlug = require('remark-slug');
const remarkFrontmatter = require('remark-frontmatter');
const remarkDisableTokenizers = require('remark-disable-tokenizers');

// rehype plugins
const rehypeSanitize = require('rehype-sanitize');
Expand Down Expand Up @@ -61,14 +62,14 @@ const tableFlattening = require('./processor/plugin/table-flattening');
const toPlainText = require('./processor/plugin/plain-text');

// Processor Option Defaults
const options = require('./options.json');
const { options, parseOptions } = require('./options.js');

/**
* Normalize Magic Block Raw Text
*/
export function setup(blocks, opts = {}) {
// merge default and user options
opts = { ...options, ...opts };
opts = parseOptions(opts);

// normalize magic block linebreaks
if (opts.normalize && blocks) {
Expand Down Expand Up @@ -110,6 +111,7 @@ export function processor(opts = {}) {
* - sanitize and remove any disallowed attributes
* - output the hast to a React vdom with our custom components
*/

return unified()
.use(remarkParse, opts.markdownOptions)
.use(remarkFrontmatter, ['yaml', 'toml'])
Expand All @@ -118,6 +120,7 @@ export function processor(opts = {}) {
.use(!opts.correctnewlines ? remarkBreaks : () => {})
.use(customParsers)
.use(remarkSlug)
.use(remarkDisableTokenizers, opts.disableTokenizers)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeSanitize, sanitize);
Expand Down
83 changes: 83 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const options = {
compatibilityMode: false,
copyButtons: true,
correctnewlines: false,
markdownOptions: {
fences: true,
commonmark: true,
gfm: true,
ruleSpaces: false,
listItemIndent: '1',
spacedTable: true,
paddedTable: true,
setext: true,
},
normalize: true,
settings: {
position: false,
},
};

// NOTE: disabling newline, paragraph, or text trips remark into an infinite loop!
const blocks = [
// 'newline',
'indentedCode',
'fencedCode',
'blockquote',
'atxHeading',
'thematicBreak',
'list',
'setextHeading',
'html',
'footnote',
'definition',
'table',
// 'paragraph',
];

const inlines = [
'escape',
'autoLink',
'url',
'html',
'link',
'reference',
'strong',
'emphasis',
'deletion',
'code',
'break',
// 'text',
];

const toBeDecorated = {
inlines: inlines.filter(i => !['link', 'reference'].includes(i)),
blocks: [],
};

const disableTokenizers = {
blocks: {
disableTokenizers: {
inline: toBeDecorated.inlines,
block: toBeDecorated.blocks,
},
},
inlines: {
disableTokenizers: {
inline: inlines.filter(i => !toBeDecorated.inlines.includes(i)),
block: blocks.filter(b => !toBeDecorated.blocks.includes(b)),
},
},
};

const parseOptions = (opts = {}) => {
if (opts.tokenizerSet in disableTokenizers) {
return { ...disableTokenizers[opts.tokenizerSet], ...opts };
} else if (opts.tokenizerSet) {
throw new Error(`opts.tokenizerSet "${opts.tokenizerSet}" not one of "${Object.keys(disableTokenizers)}"`);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename the public-facing option to disableTokenizers? It feels a bit redundant here, but I think it'd be a bit more descriptive of what it actually does for the end user.

Suggested change
if (opts.tokenizerSet in disableTokenizers) {
return { ...disableTokenizers[opts.tokenizerSet], ...opts };
} else if (opts.tokenizerSet) {
throw new Error(`opts.tokenizerSet "${opts.tokenizerSet}" not one of "${Object.keys(disableTokenizers)}"`);
}
if (opts.disableTokenizers in disableTokenizers) {
return { ...disableTokenizers[opts.disableTokenizers], ...opts };
} else if (opts.disableTokenizers) {
throw new Error(`opts.disableTokenizers "${opts.disableTokenizers}" not one of "${Object.keys(disableTokenizers)}"`);
}


return opts;
};

export { options, parseOptions };
19 changes: 0 additions & 19 deletions options.json

This file was deleted.

Loading