Skip to content

Commit

Permalink
chore: allow disabling per tokenizer (#136)
Browse files Browse the repository at this point in the history
| Fixes RM-541 |
|:---:|

## 🧰 Changes

Allow fine grain control over which tokenizers are in use. While developing for @readme/editor, we'd like to keep the editor non-lossy. The easiest way to do that is to disable tokenizers that we don't yet handle.

If we don't disable a tokenizer, we could convert it back to plain text. But it gets complicated, because we'd have to know what text was 'eaten' by the markdown engine.
  • Loading branch information
kellyjosephprice committed Mar 11, 2021
1 parent 46f976d commit 9bdd6e7
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 9 deletions.
34 changes: 34 additions & 0 deletions __tests__/__snapshots__/disabling-tokenizers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,37 @@ Object {
"type": "root",
}
`;

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

exports[`disableTokenizers: {} disables an inline tokenizer 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "\`const js = true\`",
},
],
"type": "paragraph",
},
],
"type": "root",
}
`;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions __tests__/disabling-tokenizers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ describe('disableTokenizers: "blocks"', () => {
expect(markdown.mdast(md, opts)).toMatchSnapshot();
});
});

describe('disableTokenizers: {}', () => {
it('disables a block tokenizer', () => {
const opts = { disableTokenizers: { block: ['atxHeading'] } };
const md = '# heading 1';
expect(markdown.mdast(md, opts)).toMatchSnapshot();
});

it('disables an inline tokenizer', () => {
const opts = { disableTokenizers: { inline: ['code'] } };
const md = '`const js = true`';
expect(markdown.mdast(md, opts)).toMatchSnapshot();
});
});
24 changes: 19 additions & 5 deletions docs/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,22 @@ Tables have been simplified to mirror a more standard implementation. We've also
.markdown-body .rdmd-table th {}
.markdown-body .rdmd-table td {}
```
[block:html]
{
"html": "<style>\n .markdown-body .rdmd-table {\n --table-text: black;\n --table-head: #5b1c9f;\n --table-head-text: white;\n --table-stripe: #f0eaf7;\n --table-edges: rgba(34, 5, 64, .5);\n --table-row: white;\n \n --markdown-radius: 3px;\n }\n</style>"
}
[/block]

<style>
.markdown-body .rdmd-table {
--table-text: black;
--table-head: #5b1c9f;
--table-head-text: white;
--table-stripe: #f0eaf7;
--table-edges: rgba(34, 5, 64, .5);
--table-row: white;
}

#rdmd-demo .markdown-body .rdmd-table thead tr {
box-shadow: none;
}

#rdmd-demo .markdown-body .rdmd-table thead tr th:last-child {
box-shadow: none;
}
</style>
12 changes: 8 additions & 4 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ const parseOptions = (userOpts = {}) => {

if (opts.disableTokenizers in disableTokenizers) {
opts = { ...opts, ...disableTokenizers[opts.disableTokenizers] };
} else if (opts.disableTokenizers) {
throw new Error(
`opts.disableTokenizers "${opts.disableTokenizers}" not one of "${Object.keys(disableTokenizers)}"`
);
}
// @note: commenting out for now. While doing dev for @readme/editor, I would
// like fine-grained control of which tokenizers we are using. We might to
// remove that at some point?
// } else if (opts.disableTokenizers) {
// throw new Error(
// `opts.disableTokenizers "${opts.disableTokenizers}" not one of "${Object.keys(disableTokenizers)}"`
// );
// }

return opts;
};
Expand Down

0 comments on commit 9bdd6e7

Please sign in to comment.