Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,30 @@ export const gfmStrikethroughFromMarkdown = {
exit: {strikethrough: exitStrikethrough}
}

/**
* List of constructs that occur in phrasing (paragraphs, headings), but cannot
* contain strikethroughs. So they sort of cancel each other out.
*
* Note: keep in sync with: <https://github.com/syntax-tree/mdast-util-to-markdown/blob/c47743b/lib/unsafe.js#L11>
*/
const constructsWithoutStrikethrough = [
'autolink',
'destinationLiteral',
'destinationRaw',
'reference',
'titleQuote',
'titleApostrophe'
]

/** @type {ToMarkdownExtension} */
export const gfmStrikethroughToMarkdown = {
unsafe: [{character: '~', inConstruct: 'phrasing'}],
unsafe: [
{
character: '~',
inConstruct: 'phrasing',
notInConstruct: constructsWithoutStrikethrough
}
],
handlers: {delete: handleDelete}
}

Expand Down
96 changes: 96 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,101 @@ test('mdast -> markdown', (t) => {
'should serialize strikethrough w/ eols'
)

t.equal(
toMarkdown(
{
type: 'paragraph',
children: [
{
type: 'link',
url: '~a',
children: []
}
]
},
{extensions: [gfmStrikethroughToMarkdown]}
),
'[](~a)\n',
'should not escape tildes in a `destinationLiteral`'
)

t.equal(
toMarkdown(
{
type: 'paragraph',
children: [
{
type: 'link',
url: '~a',
children: [{type: 'text', value: 'link text'}]
}
]
},
{extensions: [gfmStrikethroughToMarkdown]}
),
'[link text](~a)\n',
'should not escape tildes in a `destinationRaw`'
)

t.equal(
toMarkdown(
{
type: 'paragraph',
children: [
{
type: 'linkReference',
identifier: '~a',
referenceType: 'full',
children: []
}
]
},
{extensions: [gfmStrikethroughToMarkdown]}
),
'[][~a]\n',
'should not escape tildes in a `reference`'
)

t.equal(
toMarkdown(
{
type: 'paragraph',
children: [
{
type: 'link',
url: '#',
title: '~a',
children: []
}
]
},
{extensions: [gfmStrikethroughToMarkdown]}
),
'[](# "~a")\n',
'should not escape tildes in a `title` (double quotes)'
)

t.equal(
toMarkdown(
{
type: 'paragraph',
children: [
{
type: 'link',
url: '#',
title: '~a',
children: []
}
]
},
{
quote: "'",
extensions: [gfmStrikethroughToMarkdown]
}
),
"[](# '~a')\n",
'should not escape tildes in a `title` (single quotes)'
)

t.end()
})