-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.ts
145 lines (130 loc) · 5.45 KB
/
utilities.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { HTMLDocument } from "./deps/deno_dom.ts";
import { MarkdownIt } from "./deps/markdown-it/mod.ts";
import { distance } from "./deps/jaro-winkler.ts";
import { getFetchWithRetries } from "./fetch.ts";
import type { MarkdownItToken } from "./types.ts";
import { FETCH_OPTIONS } from "./constants.ts";
const RETRY_FAILED_FETCH = true;
const MAX_RETRIES = 5;
const ID_TAGS = ["section", "h1", "h2", "h3", "h4", "h5", "h6", "div", "a"];
const MINIMUM_DISTANCE = 0.8;
export const fetchWithRetries = getFetchWithRetries(RETRY_FAILED_FETCH, MAX_RETRIES, FETCH_OPTIONS);
export function parseMarkdownContent(mdit: MarkdownIt, content: string) {
const html = mdit.render(content, {});
const tokens = mdit.parse(content, {});
const links = filterLinksFromTokens(tokens);
return { links, html };
}
export function getAnchors(
document: HTMLDocument,
opts: { includeHref: boolean } = { includeHref: true },
): Set<string> {
const anchors: string[] = [];
for (const tag of ID_TAGS) {
const ids = document.getElementsByTagName(tag)
.map((element) => element.getAttribute("id"))
.filter((id) => id != null && id.trim() !== "") as string[];
anchors.push(...ids);
}
return new Set([
...anchors,
...(opts.includeHref
? document.getElementsByTagName("a")
.map((element) => element.getAttribute("href"))
.filter((href) => href != null && href.startsWith("#") && href.length > 1)
.map((href) => href!.substring(1))
: []),
]);
}
export function parseLink(href: string): { root: string; anchor?: string } {
// if (!URL.canParse(href)) { // looks like an local relative link; TODO: add additional checks
// const hashPos = href.indexOf("#");
// if (hashPos === -1) return { root: href };
// return { root: href.substring(0, hashPos), anchor: decodeURIComponent(href.substring(hashPos + 1)) };
// }
// not a relative link, hopefully external.
const hashPos = href.indexOf("#");
if (hashPos == -1) return { root: href };
return { root: href.substring(0, hashPos), anchor: decodeURIComponent(href.substring(hashPos + 1)) };
}
function filterLinksFromTokens(tokens: MarkdownItToken[]) {
const links: string[] = [];
for (const token of tokens) {
if (token.type === "link_open") {
const href = token.attrGet("href");
if (href != null) links.push(decodeURIComponent(href));
}
if (token.children != null) {
links.push(...filterLinksFromTokens(token.children));
}
}
return new Set(links);
}
export function getPossibleMatches(anchor: string, allAnchors: Set<string>) {
const matches: string[] = [];
for (const possible of allAnchors) {
const percent = distance(anchor.toLowerCase(), possible.toLowerCase());
if (percent >= MINIMUM_DISTANCE) matches.push(possible);
}
return matches;
}
function getColumns(haystack: string, needle: string) {
const indices: number[] = [];
if (needle === "") throw new Error("Unexpected needle: an <empty string>???");
while (haystack.includes(needle)) {
const length = indices.push(haystack.indexOf(needle) + 1);
haystack = haystack.slice(indices[length - 1]);
}
return indices;
}
// little grep (my own impl.)
export async function findStringLocations(
filepath: string,
searchString: string,
): Promise<[line: number, columns: number[], text: string][]> {
using file = await Deno.open(filepath, { read: true });
let tempLine = "";
let currentLine = 1;
const locations: [line: number, columns: number[], text: string][] = [];
const decoder = new TextDecoder();
for await (const chunk of file.readable) {
const decodedChunk = decoder.decode(chunk);
const lines = decodedChunk.split("\n");
tempLine += lines.shift();
if (lines.length <= 1) continue;
if (tempLine.includes(searchString)) {
locations.push([currentLine, getColumns(tempLine, searchString), tempLine]);
}
currentLine++;
tempLine = lines.pop()!;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.includes(searchString)) {
locations.push([currentLine, getColumns(line, searchString), line]);
}
currentLine++;
}
}
return locations;
}
export function indentText(text: string, indentSize: number) {
const indent = " ".repeat(indentSize);
return text.includes("\n") ? text.split("\n").map((line) => indent + line).join("\n") : indent + text;
}
export function getEnv<T extends string>(optional: boolean, ...vars: T[]) {
return vars.reduce((result, variable): Record<T, string> => {
const value = Deno.env.get(variable);
if (!optional && value == null) throw new Error("Missing env var: " + variable);
return { ...result, [variable]: value };
}, {} as Record<T, string>);
}
export function execute(command: string[], options?: Omit<Deno.CommandOptions, "args">) {
return new Deno.Command(command[0], { ...options, args: command.slice(1) });
}
export async function getCommitSha(repository: string) {
const output = (await execute(["git", "rev-parse", "HEAD"], { cwd: repository }).output()).stdout;
return new TextDecoder().decode(output).trim();
}
export function sleep(ms: number): Promise<unknown> {
return new Promise((resolve) => setTimeout(resolve, ms));
}