From 8d2ea46784b8e039133caea1c8b1990ec39b2ebc Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sat, 25 Dec 2021 00:30:35 -0500 Subject: [PATCH] [+] Markdown: add() --- src/scripts/extended_markdown.ts | 45 +++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/scripts/extended_markdown.ts b/src/scripts/extended_markdown.ts index 7915f50..6f1ef6b 100644 --- a/src/scripts/extended_markdown.ts +++ b/src/scripts/extended_markdown.ts @@ -3,7 +3,7 @@ * Regex used in markdown extension parsing */ const re = { - command: /)/g, + command: //g, hashes: /^#+/g, } @@ -19,6 +19,25 @@ export function parseExtensions(raw: string): string let lines = raw.replace('\r\n', '\n').split('\n') let i = 0 + /** + * Find the end index of a section or -1 + */ + function findSectionEnd(): number + { + const r = re.hashes.exec(lines[i]) + if (!r) return -1 + const level = r[0].length + + // Find next same-level or higher-level header's index + let j = i + 1 + for (; j < lines.length; j++) + { + const r = re.hashes.exec(lines[j]) + if (r && r[0].length <= level) break + } + return j + } + /** * Hide an entire hn section * Example: @@ -31,18 +50,17 @@ export function parseExtensions(raw: string): string */ function hideSection() { - const r = re.hashes.exec(lines[i]) - if (!r) return - const level = r[0].length + const e = findSectionEnd() + if (e != -1) lines.splice(i, e - i) + } - // Find next same-level or higher-level header's index - let j = i + 1 - for (; j < lines.length; j++) - { - const r = re.hashes.exec(lines[j]) - if (r && r[0].length <= level) break - } - lines.splice(i, j - i) + /** + * Add something + * @param s String + */ + function add(s: string) + { + lines[i].replace(re.command, s) } // Run all commands in markdown @@ -54,7 +72,8 @@ export function parseExtensions(raw: string): string const r = re.command.exec(lines[i]) if (r) { - const cmd = r[0].substring(5).trim() + let cmd = r[0] + cmd = cmd.substring(5, cmd.length - 5).trim() // Run cmd console.log(`Running command`, cmd)