forked from code-hike/codehike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd-utils.mjs
More file actions
55 lines (50 loc) · 1.38 KB
/
Copy pathmd-utils.mjs
File metadata and controls
55 lines (50 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { unified } from "unified"
import remarkParse from "remark-parse"
import remarkStringify from "remark-stringify"
import * as mdastToString from "mdast-util-to-string"
const BumpLevels = {
dep: 0,
patch: 1,
minor: 2,
major: 3,
}
export function getChangelogEntry(changelog, version) {
let ast = unified().use(remarkParse).parse(changelog)
let highestLevel = BumpLevels.dep
let nodes = ast.children
let headingStartInfo
let endIndex
for (let i = 0; i < nodes.length; i++) {
let node = nodes[i]
if (node.type === "heading") {
let stringified = mdastToString.toString(node)
let match = stringified.toLowerCase().match(/(major|minor|patch)/)
if (match !== null) {
let level = BumpLevels[match[0]]
highestLevel = Math.max(level, highestLevel)
}
if (headingStartInfo === undefined && stringified === version) {
headingStartInfo = {
index: i,
depth: node.depth,
}
continue
}
if (
endIndex === undefined &&
headingStartInfo !== undefined &&
headingStartInfo.depth === node.depth
) {
endIndex = i
break
}
}
}
if (headingStartInfo) {
ast.children = ast.children.slice(headingStartInfo.index + 1, endIndex)
}
return {
content: unified().use(remarkStringify).stringify(ast),
highestLevel: highestLevel,
}
}