MCPcopy Create free account
hub / github.com/ZenNotes/zennotes / codeRanges

Function codeRanges

packages/app-core/src/lib/transclusion.ts:29–41  ·  view source on GitHub ↗

Character ranges of fenced + inline code, so we never expand `![[…]]` there.

(md: string)

Source from the content-addressed store, hash-verified

27
28/** Character ranges of fenced + inline code, so we never expand `![[…]]` there. */
29function codeRanges(md: string): Array<[number, number]> {
30 const ranges: Array<[number, number]> = []
31 // Fenced code blocks (``` or ~~~), including indistinct fences.
32 const fence = /^([ \t]*)(`{3,}|~{3,})[^\n]*\n[\s\S]*?(?:\n\1\2[^\n]*|$)/gm
33 for (const m of md.matchAll(fence)) ranges.push([m.index!, m.index! + m[0].length])
34 // Inline code spans (`…`), skipping ones already inside a fence.
35 const inline = /`+[^`\n]*`+/g
36 for (const m of md.matchAll(inline)) {
37 const at = m.index!
38 if (!ranges.some(([s, e]) => at >= s && at < e)) ranges.push([at, at + m[0].length])
39 }
40 return ranges
41}
42
43function inCode(index: number, ranges: Array<[number, number]>): boolean {
44 return ranges.some(([s, e]) => index >= s && index < e)

Callers 2

expandFunction · 0.85
hasNoteEmbedsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected