rvlib-shipkit 2.25.0 → 2.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,6 +32,32 @@ export type MdScan = {
32
32
  dangling: MdDanglingRef[]
33
33
  }
34
34
 
35
+ /** the anchor a heading answers to, as the forges write it: lower case, its marks dropped, each space a dash */
36
+ const slugOf = (heading: string): string =>
37
+ heading.trim().toLowerCase().replace(/\[([^\]]*)\]\([^)]*\)/g, '$1').replace(/[^\p{L}\p{N}\p{M}_\- ]/gu, '').replace(/ /g, '-')
38
+
39
+ /** an anchor as a link writes it, read the way a heading's slug is */
40
+ export const anchorSlug = (anchor: string): string => {
41
+ try { return decodeURIComponent(anchor).toLowerCase() } catch { return anchor.toLowerCase() }
42
+ }
43
+
44
+ /** every anchor a page's headings answer to; a heading that repeats takes `-1`, `-2` after its slug. Fenced code holds no heading */
45
+ export function headingSlugs(text: string): Set<string> {
46
+ const out = new Set<string>()
47
+ const seen = new Map<string, number>()
48
+ let fenced = false
49
+ for (const line of text.split('\n')) {
50
+ if (line.startsWith('```')) { fenced = !fenced; continue }
51
+ const m = fenced ? null : /^#{1,6}\s+(.*?)\s*#*\s*$/.exec(line)
52
+ if (m == null) continue
53
+ const s = slugOf(m[1]!)
54
+ const n = seen.get(s) ?? 0
55
+ seen.set(s, n + 1)
56
+ out.add(n === 0 ? s : `${s}-${n}`)
57
+ }
58
+ return out
59
+ }
60
+
35
61
  export function scanMdGraph(cfg: ScanMdConfig): MdGraph {
36
62
  return scanMd(cfg).graph
37
63
  }
@@ -104,6 +130,12 @@ function scanMdNow(cfg: ScanMdConfig): MdScan {
104
130
  if (!inRoot && inBoundary && existsSync(cand)) return { to: [], onDisk: true }
105
131
  return null
106
132
  }
133
+ const headings = new Map<string, Set<string>>()
134
+ const headingsOf = (page: string): Set<string> => {
135
+ let h = headings.get(page)
136
+ if (h == null) { h = headingSlugs(readFileSync(join(cfg.root, page), 'utf8')); headings.set(page, h) }
137
+ return h
138
+ }
107
139
  const edgesOf = (rel: string): MdEdge[] => {
108
140
  const raw = readFileSync(join(cfg.root, rel), 'utf8')
109
141
  const body = raw.startsWith('---\n') ? raw.slice(4).replace(/^[\s\S]*?^---\s*$/m, '') : raw
@@ -122,8 +154,16 @@ function scanMdNow(cfg: ScanMdConfig): MdScan {
122
154
  }
123
155
  for (const m of prose.matchAll(/(?:^|[\s(])@((?:\.{1,2}\/)*[\w][\w./-]*\.md)/gm)) push(m[1]!, true, true, lineOf(m))
124
156
  for (const m of prose.matchAll(/→\s*([\w][\w./-]*\.md)\b/g)) push(m[1]!, false, true, lineOf(m))
125
- // an anchor or a title never changes the page a link reaches
126
- for (const m of prose.matchAll(/\]\(([^)\s:#]+\.md)(?:#[^)\s]*)?(?:\s+"[^"]*")?\)/g)) push(m[1]!, false, true, lineOf(m))
157
+ // an anchor or a title never changes the page a link reaches; an anchor names a heading of that page, and one
158
+ // the page does not have is a ref of its own that dangles (a heading renamed under its links)
159
+ for (const m of prose.matchAll(/\]\(([^)\s:#]+\.md)?(?:#([^)\s]*))?(?:\s+"[^"]*")?\)/g)) {
160
+ if (m[1] == null && m[2] == null) continue
161
+ if (m[1] != null) push(m[1], false, true, lineOf(m))
162
+ if (m[2] == null || m[2] === '') continue
163
+ const to = m[1] == null ? rel : resolveRef(rel, m[1])?.to
164
+ const page = typeof to === 'string' ? to : to?.length === 1 ? to[0]! : null
165
+ if (page != null && !headingsOf(page).has(anchorSlug(m[2]))) dangling.push({ from: rel, token: `${m[1] ?? ''}#${m[2]}`, line: lineOf(m) })
166
+ }
127
167
  for (const m of prose.matchAll(/\]\(([^)\s:]*\/)\)/g)) push(m[1]!, false, true, lineOf(m))
128
168
  for (const s of spans) for (const m of s.matchAll(/[\w][\w./-]*\.md/g)) push(m[0], false, false)
129
169
  for (const s of spans) for (const m of s.matchAll(/(?:^|\s)([\w.][\w./-]*\/)(?=$|[\s<*])/g)) push(m[1]!, false, false)