From 67d858983c5bde3cd8ce432da5d92b812884a8e6 Mon Sep 17 00:00:00 2001 From: TitiAlone Date: Mon, 8 Mar 2021 22:30:12 +0100 Subject: [PATCH 1/2] [zmarkdown] Add a test case for #431 --- .../zmarkdown/__tests__/regressions.test.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/packages/zmarkdown/__tests__/regressions.test.js b/packages/zmarkdown/__tests__/regressions.test.js index 3e7d9382e..776e6bbcd 100644 --- a/packages/zmarkdown/__tests__/regressions.test.js +++ b/packages/zmarkdown/__tests__/regressions.test.js @@ -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', () => { From 352a34f73d66d64f3386569cb510ee56b1f37457 Mon Sep 17 00:00:00 2001 From: TitiAlone Date: Mon, 8 Mar 2021 22:31:04 +0100 Subject: [PATCH 2/2] [zmarkdown] Fix too much randomness in footnotes --- packages/zmarkdown/common.js | 6 ++++++ packages/zmarkdown/config/html/index.js | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/zmarkdown/common.js b/packages/zmarkdown/common.js index b252d4a6d..5b625ba73 100644 --- a/packages/zmarkdown/common.js +++ b/packages/zmarkdown/common.js @@ -33,10 +33,15 @@ 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) @@ -44,6 +49,7 @@ module.exports = ( } parser.process(input, (err, vfile) => { + if (doRegenerate) regenerator() if (err) return cb(err) cb(null, vfile) diff --git a/packages/zmarkdown/config/html/index.js b/packages/zmarkdown/config/html/index.js index 268d7d4c0..9108c7c93 100644 --- a/packages/zmarkdown/config/html/index.js +++ b/packages/zmarkdown/config/html/index.js @@ -1,5 +1,7 @@ const shortid = require('shortid') +let currentFootnotePostfix = shortid.generate() + module.exports = { autolinkHeadings: { behaviour: 'append', @@ -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'),