Skip to content

Commit

Permalink
␡ Enable strikethrough as markdownit extension (#1427)
Browse files Browse the repository at this point in the history
Co-authored-by: Rowan Cockett <rowanc1@gmail.com>
  • Loading branch information
sglyon and rowanc1 authored Aug 21, 2024
1 parent fab598e commit 1a5f3f3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-fireants-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-parser": patch
---

Enable strikethrough as markdownit extension
1 change: 1 addition & 0 deletions packages/myst-parser/src/fromMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export type AllOptions = {
tasklist?: boolean;
tables?: boolean;
blocks?: boolean;
strikethrough?: boolean;
};
mdast: MdastOptions;
directives: DirectiveSpec[];
Expand Down
2 changes: 2 additions & 0 deletions packages/myst-parser/src/myst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const defaultOptions: Omit<AllOptions, 'vfile'> = {
tasklist: true,
tables: true,
blocks: true,
strikethrough: false,
},
mdast: {},
directives: defaultDirectives,
Expand Down Expand Up @@ -75,6 +76,7 @@ export function createTokenizer(opts?: Options) {
if (markdownit.linkify) {
tokenizer.linkify.tlds(tlds.filter((tld) => !EXCLUDE_TLDS.includes(tld)));
}
if (extensions.strikethrough) tokenizer.enable('strikethrough');
if (extensions.smartquotes) tokenizer.enable('smartquotes');
if (extensions.tables) tokenizer.enable('table');
if (extensions.colonFences) tokenizer.use(colonFencePlugin);
Expand Down
1 change: 1 addition & 0 deletions packages/myst-parser/src/tokensToMyst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const defaultMdast: Record<string, TokenHandlerSpec> = {
};
},
},
s: { type: 'delete' },
hr: {
type: 'thematicBreak',
noCloseToken: true,
Expand Down
42 changes: 42 additions & 0 deletions packages/myst-parser/tests/strikethrough.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, it } from 'vitest';
import { visit } from 'unist-util-visit';
import { mystParse } from '../src';
import type { GenericParent } from 'myst-common';

function stripPositions(tree: GenericParent) {
visit(tree, (node) => {
delete node.position;
});
return tree;
}

describe('strikethrough', () => {
it('strikethrough in paragraph', () => {
const content = 'A ~~div~~ paragraph!';
expect(stripPositions(mystParse(content))).toEqual({
type: 'root',
children: [
{
type: 'paragraph',
children: [{ type: 'text', value: 'A ~~div~~ paragraph!' }],
},
],
});
expect(stripPositions(mystParse(content, { extensions: { strikethrough: true } }))).toEqual({
type: 'root',
children: [
{
type: 'paragraph',
children: [
{ type: 'text', value: 'A ' },
{
type: 'delete',
children: [{ type: 'text', value: 'div' }],
},
{ type: 'text', value: ' paragraph!' },
],
},
],
});
});
});

0 comments on commit 1a5f3f3

Please sign in to comment.