Skip to content

Commit

Permalink
Merge pull request #432 from StaloneLab/fix-footnotes-the-return-none
Browse files Browse the repository at this point in the history
  • Loading branch information
StaloneLab committed Mar 9, 2021
2 parents 4cba823 + 352a34f commit 26c2b32
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
35 changes: 35 additions & 0 deletions packages/zmarkdown/__tests__/regressions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,41 @@ describe('Regression tests', () => {
const [rendered] = response.data
expect(rendered).toMatchSnapshot()
})

test('#431 It uses random footnote postfix correctly', async () => {
const md = dedent`
hello[^foo] world[^bar]
[^foo]: foo
[^bar]: bar
`

const firstResponse = await a.post(html, {
md,
opts: {},
})

const secondResponse = await a.post(html, {
md,
opts: {},
})

const [firstRendered] = firstResponse.data
const [secondRendered] = secondResponse.data

const firstFooReference = firstRendered.match(/fnref-1-([a-zA-Z0-9-_]+)/)
const firstFooDefinition = firstRendered.match(/fn-1-([a-zA-Z0-9-_]+)/)
const firstBarReference = firstRendered.match(/fnref-2-([a-zA-Z0-9-_]+)/)
const secondBarDefinition = secondRendered.match(/fn-2-([a-zA-Z0-9-_]+)/)

// A unique token shall be generated for a given footnote
expect(firstFooReference[1]).toEqual(firstFooDefinition[1])
// But also for a given render
expect(firstFooReference[1]).toEqual(firstBarReference[1])

// Finally, we expect tokens to differ between two successive renderings
expect(secondBarDefinition[1]).not.toEqual(firstBarReference[1])
})
})

describe('Latex endpoint', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/zmarkdown/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ module.exports = (
parser.use(processor, processorConfig)
}

// Regenerate footnotes postfix on extracts
const doRegenerate = (processor === 'html' && processorConfig._regenerateFootnotePostfix)
const regenerator = processorConfig._regenerateFootnotePostfix

return (input, cb) => {
if (typeof cb !== 'function') {
return new Promise((resolve, reject) =>
parser.process(input, (err, vfile) => {
if (doRegenerate) regenerator()
if (err) return reject(err)

resolve(vfile)
}))
}

parser.process(input, (err, vfile) => {
if (doRegenerate) regenerator()
if (err) return cb(err)

cb(null, vfile)
Expand Down
8 changes: 7 additions & 1 deletion packages/zmarkdown/config/html/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const shortid = require('shortid')

let currentFootnotePostfix = shortid.generate()

module.exports = {
autolinkHeadings: {
behaviour: 'append',
Expand All @@ -22,7 +24,11 @@ module.exports = {

sanitize: require('../sanitize'),

postfixFootnotes: (agg) => `${agg}-${shortid.generate()}`,
postfixFootnotes: (agg) => `${agg}-${currentFootnotePostfix}`,

_regenerateFootnotePostfix: () => {
currentFootnotePostfix = shortid.generate()
},

postProcessors: {
iframeWrappers: require('./iframe-wrappers'),
Expand Down

0 comments on commit 26c2b32

Please sign in to comment.