From eece55730545c143351b632825e7e6ece31b1a9a Mon Sep 17 00:00:00 2001 From: Elihuso Quigley Date: Fri, 17 May 2024 20:28:56 +0800 Subject: [PATCH] [+] banner --- scripts/build.ts | 37 ++------------------------------ scripts/data.ts | 6 ++++++ scripts/feature.ts | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 35 deletions(-) create mode 100644 scripts/data.ts create mode 100644 scripts/feature.ts diff --git a/scripts/build.ts b/scripts/build.ts index cc7f2b1a..b74d5e8a 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -8,7 +8,7 @@ import metadataParser from 'markdown-yaml-metadata-parser'; import { renderMdx } from "./mdx.js"; import moment from "moment"; -import { Icon, backSVG } from "./icon.js"; +import { handleFeatures } from "./feature.js"; const PUBLIC_DIR = "public"; @@ -150,14 +150,7 @@ 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) - - // Handle Delete Line: ~~something~~ to something - markdown = handleDeleteLine(markdown) - - // Handle Icon - markdown = handleNoteIcon(markdown) + markdown = handleFeatures(markdown) // Autocorrect markdown markdown = autocorrect.formatFor(markdown, 'markdown') @@ -172,32 +165,6 @@ function buildPeoplePages() { } } -function handleFootnote(md: string) { - if (!md.includes('[^')) return md - - // Replace footnote references with HTML superscript tags - return md.replace(/\[\^(\d+)\](?::\s*(.*))?/g, (match, id, text) => text ? - // Footnote definition - `
  • ${text}${backSVG}
  • ` : - // Footnote reference - `${id}` - ) - - // Wrap the footnote definitions in an ordered list - .replace(/(
  • " + text + "")); -} - -function handleNoteIcon(md: string): string { - if (!md.includes('[!')) return md; - return md.replace(/\[\!(\w+)\](?::\s*(.*))?/g, (match, icon, _) => (Icon[icon as string])); -} - // Copy `people/${dirname}/photos` to `dist/people/${dirname}/`. function copyPeopleAssets() { const PEOPLE_ASSETS = ["photos", "backup", "page.md"]; diff --git a/scripts/data.ts b/scripts/data.ts new file mode 100644 index 00000000..00acacb2 --- /dev/null +++ b/scripts/data.ts @@ -0,0 +1,6 @@ +export interface BannerData { + type: string, + icon: string, + title: string, + text: string +} \ No newline at end of file diff --git a/scripts/feature.ts b/scripts/feature.ts new file mode 100644 index 00000000..a92414fa --- /dev/null +++ b/scripts/feature.ts @@ -0,0 +1,53 @@ +import { Icon, backSVG } from "./icon.js"; +import { BannerData } from "./data.js"; + +function handleFootnote(md: string) { + if (!md.includes("[^")) return md; + + // Replace footnote references with HTML superscript tags + return ( + md.replace(/\[\^(\d+)\](?::\s*(.*))?/g, (match, id, text) => text ? // Footnote definition + `
  • ${text}${backSVG}
  • ` : // Footnote reference + `${id}` + ) + + // Wrap the footnote definitions in an ordered list + .replace(/(
  • \n$1\n") + ); +} + +function handleDeleteLine(md: string): string { + if (!md.includes("~~")) return md; + + return md.replace(/~~(.*?)~~/g, (match, text) => "" + text + ""); +} + +function handleNoteIcon(md: string): string { + if (!md.includes("[!")) return md; + return md.replace(/\[\!(\w+)\](?::\s*(.*))?/g, (match, icon, _) => Icon[icon as string]); +} + +function handleBanner(md: string): string { + if (!md.includes('[[')) return md; + return md.replace(/\[\[(.*?)\]\]/g, (match, raw) => { + const data = JSON.parse(raw) as BannerData + if (data.type != 'banner') return match + return `

    ${data.title}

    ${data.text}

    ` + }) +} + +export function handleFeatures(markdown: string): string { + // Handle Footnote + let md = handleFootnote(markdown) + + // Handle Delete Line: ~~something~~ to something + md = handleDeleteLine(md) + + // Handle Icon + md = handleNoteIcon(md) + + // Handle Banner + md = handleBanner(md) + + return md +} \ No newline at end of file