-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { destr } from "destr"; | ||
|
||
const AUTOMD_RE = | ||
/^(?<open><!--\s*AUTOMD_START\s*(?<args>.*?)\s*-->)(?<contents>.+?)(?<close><!--\s*AUTOMD_END\s*-->)/gms; | ||
|
||
export function findAutoMdBlocks(md: string) { | ||
const blocks: { | ||
loc: { start: number; end: number }; | ||
rawArgs: string; | ||
contents: string; | ||
}[] = []; | ||
|
||
for (const match of md.matchAll(AUTOMD_RE)) { | ||
if (match.index === undefined || !match.groups) { | ||
continue; | ||
} | ||
|
||
const start = match.index + match.groups.open.length; | ||
const end = start + match.groups.contents.length; | ||
|
||
blocks.push({ | ||
loc: { start, end }, | ||
rawArgs: match.groups.args, | ||
contents: match.groups.contents, | ||
}); | ||
} | ||
|
||
return blocks; | ||
} | ||
|
||
export function parseRawArgs(rawArgs: string) { | ||
return Object.fromEntries( | ||
[...rawArgs.matchAll(/(?<key>\w+)=?(["'])?(?<value>[^\2]+?)\2/g)].map( | ||
(m) => [m.groups?.key, destr(m.groups?.value)], | ||
), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters