Skip to content

Commit

Permalink
Remove warnings on lists, colons
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 31, 2022
1 parent e236216 commit ae46cf7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 55 deletions.
38 changes: 8 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import {containerFlow} from 'mdast-util-to-markdown/lib/util/container-flow.js'
import {indentLines} from 'mdast-util-to-markdown/lib/util/indent-lines.js'
import {safe} from 'mdast-util-to-markdown/lib/util/safe.js'
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
import {visit, EXIT} from 'unist-util-visit'

let warningColonInFootnote = false
let warningListInFootnote = false

/**
* @returns {FromMarkdownExtension}
Expand Down Expand Up @@ -144,41 +140,23 @@ export function gfmFootnoteToMarkdown() {
let value = tracker.move('[^')
const exit = context.enter('footnoteDefinition')
const subexit = context.enter('label')
const id = safe(context, association(node), {
...tracker.current(),
before: value,
after: ']'
})
value += tracker.move(id)
value += tracker.move(
']:' + (node.children && node.children.length > 0 ? ' ' : '')
safe(context, association(node), {
...tracker.current(),
before: value,
after: ']'
})
)
subexit()
value += tracker.move(
']:' + (node.children && node.children.length > 0 ? ' ' : '')
)
tracker.shift(4)
value += tracker.move(
indentLines(containerFlow(node, context, tracker.current()), map)
)
exit()

if (!warningColonInFootnote && id.includes(':')) {
console.warn(
'[mdast-util-gfm-footnote] Warning: Found a colon in footnote identifier `' +
id +
'`. GitHub currently crahes on colons in footnotes (see <https://github.com/github/cmark-gfm/issues/241> for more info)'
)
warningColonInFootnote = true
}

if (!warningListInFootnote) {
visit(node, 'list', () => {
console.warn(
'[mdast-util-gfm-footnote] Warning: Found a list in a footnote definition. GitHub currently crahes on lists in footnotes (see <https://github.com/github/cmark-gfm/issues/241> for more info)'
)
warningListInFootnote = true
return EXIT
})
}

return value

/** @type {Map} */
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
"dependencies": {
"@types/mdast": "^3.0.0",
"mdast-util-to-markdown": "^1.3.0",
"micromark-util-normalize-identifier": "^1.0.0",
"unist-util-visit": "^4.0.0"
"micromark-util-normalize-identifier": "^1.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
Expand Down
41 changes: 18 additions & 23 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,32 +269,27 @@ test('mdast -> markdown', (t) => {
'should escape what would otherwise be an footnote definition'
)

const warn = console.warn
let called = 0

console.warn = () => {
called++
}

toMarkdown(
{type: 'footnoteDefinition', identifier: 'a:b', children: []},
{extensions: [gfmFootnoteToMarkdown()]}
t.deepEqual(
toMarkdown(
{type: 'footnoteDefinition', identifier: 'a:b', children: []},
{extensions: [gfmFootnoteToMarkdown()]}
),
'[^a:b]:\n',
'should support colons in footnote definitions'
)

t.equal(called, 1, 'should warn on colons in footnote identifiers')

toMarkdown(
{
type: 'footnoteDefinition',
identifier: 'a',
children: [{type: 'list', children: []}]
},
{extensions: [gfmFootnoteToMarkdown()]}
t.deepEqual(
toMarkdown(
{
type: 'footnoteDefinition',
identifier: 'a',
children: [{type: 'list', children: [{type: 'listItem', children: []}]}]
},
{extensions: [gfmFootnoteToMarkdown()]}
),
'[^a]: *\n',
'should support lists in footnote definitions'
)

t.equal(called, 2, 'should warn on lists in footnote definitions')

console.warn = warn

t.end()
})

0 comments on commit ae46cf7

Please sign in to comment.