[+] Markdown: add()

This commit is contained in:
Hykilpikonna
2021-12-25 00:30:35 -05:00
parent 4817e8a76e
commit 8d2ea46784
+32 -13
View File
@@ -3,7 +3,7 @@
* Regex used in markdown extension parsing * Regex used in markdown extension parsing
*/ */
const re = { const re = {
command: /<!--{.*?(?=}-->)/g, command: /<!--{.*?}-->/g,
hashes: /^#+/g, hashes: /^#+/g,
} }
@@ -19,6 +19,25 @@ export function parseExtensions(raw: string): string
let lines = raw.replace('\r\n', '\n').split('\n') let lines = raw.replace('\r\n', '\n').split('\n')
let i = 0 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 * Hide an entire hn section
* Example: * Example:
@@ -31,18 +50,17 @@ export function parseExtensions(raw: string): string
*/ */
function hideSection() function hideSection()
{ {
const r = re.hashes.exec(lines[i]) const e = findSectionEnd()
if (!r) return if (e != -1) lines.splice(i, e - i)
const level = r[0].length }
// Find next same-level or higher-level header's index /**
let j = i + 1 * Add something
for (; j < lines.length; j++) * @param s String
{ */
const r = re.hashes.exec(lines[j]) function add(s: string)
if (r && r[0].length <= level) break {
} lines[i].replace(re.command, s)
lines.splice(i, j - i)
} }
// Run all commands in markdown // Run all commands in markdown
@@ -54,7 +72,8 @@ export function parseExtensions(raw: string): string
const r = re.command.exec(lines[i]) const r = re.command.exec(lines[i])
if (r) if (r)
{ {
const cmd = r[0].substring(5).trim() let cmd = r[0]
cmd = cmd.substring(5, cmd.length - 5).trim()
// Run cmd // Run cmd
console.log(`Running command`, cmd) console.log(`Running command`, cmd)