[+] Markdown: add()
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user