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 1 commit
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
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,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
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.