From 496363eaa0c71256c69d8230c61f62a6374a9423 Mon Sep 17 00:00:00 2001 From: Elihuso Quigley Date: Wed, 28 Feb 2024 22:41:06 +0800 Subject: [PATCH 1/3] [O] Handle footnote --- scripts/build.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/scripts/build.ts b/scripts/build.ts index 1c19dd26..9d5c55ab 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -120,6 +120,9 @@ function buildPeoplePages() { // Read markdown page and remove markdown meta let markdown = metadataParser(fs.readFileSync(path.join(srcPath, `page${lang}.md`), "utf-8")).content.replaceAll("", " */}"); + // Handle Footnote + markdown = handleFootnote(markdown) + // Autocorrect markdown markdown = autocorrect.formatFor(markdown, 'markdown') @@ -133,6 +136,50 @@ function buildPeoplePages() { } } +function handleFootnote(md: string) { + const data = md.split('') + let result = '' + let footnote = false + let ol = false + + for (let i = 0; i < data.length; i += 1) { + if ((data[i] == '[') && (i != data.length - 1)) { + if (data[i + 1] == '^') { + footnote = true; + i += 1 + let id = '' + while (data[++i] != ']') id += data[i] + if (data[i + 1] == ':') { + if (!ol) { + ol = !ol; + result += '
    \n' + } + //
  1. message
  2. + result += `
  3. ` + let message = '' + while (data[i] != '\n') { + if (i < data.length - 1) ++i + else break + message += data[i] + } + result += message + result += `
  4. ` + } + else { + //1 + result += `${id}` + } + } + else result += data[i] + } + else result += data[i] + } + + if (footnote) result += '\n
\n' + + return result +} + // Copy `people/${dirname}/photos` to `dist/people/${dirname}/`. function copyPeopleAssets() { const PEOPLE_ASSETS = ["photos", "backup", "page.md"]; From 94dccc0bd1dfdfc3a081e0a089473d698cad4c2e Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Wed, 28 Feb 2024 18:56:09 -0500 Subject: [PATCH 2/3] [O] Optimize footnote --- scripts/build.ts | 49 ++++++++++-------------------------------------- 1 file changed, 10 insertions(+), 39 deletions(-) diff --git a/scripts/build.ts b/scripts/build.ts index 9d5c55ab..6db2a149 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -137,47 +137,18 @@ function buildPeoplePages() { } function handleFootnote(md: string) { - const data = md.split('') - let result = '' - let footnote = false - let ol = false + if (!md.includes('[^')) return md - for (let i = 0; i < data.length; i += 1) { - if ((data[i] == '[') && (i != data.length - 1)) { - if (data[i + 1] == '^') { - footnote = true; - i += 1 - let id = '' - while (data[++i] != ']') id += data[i] - if (data[i + 1] == ':') { - if (!ol) { - ol = !ol; - result += '
    \n' - } - //
  1. message
  2. - result += `
  3. ` - let message = '' - while (data[i] != '\n') { - if (i < data.length - 1) ++i - else break - message += data[i] - } - result += message - result += `
  4. ` - } - else { - //1 - result += `${id}` - } - } - else result += data[i] - } - else result += data[i] - } + // Replace footnote references with HTML superscript tags + md = md.replace(/\[\^(\d+)\](?::\s*(.*))?/g, (match, id, text) => text ? + // Footnote definition + `
  5. ${text}
  6. ` : + // Footnote reference + `${id}` + ) - if (footnote) result += '\n
\n' - - return result + // Wrap the footnote definitions in an ordered list + return md.replace(/(
  • ${text}
  • ` : // Footnote reference `${id}` ) - + // Wrap the footnote definitions in an ordered list - return md.replace(/(
  • )/gs, '
      \n$1\n
    ') } // Copy `people/${dirname}/photos` to `dist/people/${dirname}/`.