-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck-examples.sh
More file actions
executable file
·49 lines (39 loc) · 1.46 KB
/
check-examples.sh
File metadata and controls
executable file
·49 lines (39 loc) · 1.46 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
#!/usr/bin/env bash
set -e
# Generate documentation
node --run docs
# Calculate the relative import path from a markdown file to index.ts
calculate_import_path() {
local md_file="$1"
local depth=$(echo "$md_file" | awk -F'/' '{print NF-1}')
printf '../%.0s' $(seq 1 $depth)
}
# Extract TypeScript code blocks from markdown and write them as .ts files
extract_typescript_blocks() {
local md_file="$1"
local base_name="${md_file%.*}"
local import_path="$(calculate_import_path "$md_file")index.ts"
# Extract TypeScript code blocks from markdown as JSON array
pandoc -i "$md_file" -t json |
jq -a '.blocks[] | select(.t == "CodeBlock" and .c[0][1][0] == "ts") | .c[1]' |
jq -s >"${base_name}.tmp"
# Convert each code block to a standalone TypeScript file
node <<-EOF
const fs = require('node:fs');
const codeBlocks = JSON.parse(fs.readFileSync('${base_name}.tmp', 'ascii'));
codeBlocks.forEach((code, index) => {
const hasImport = code.includes('import * as HPKE');
const content = hasImport
? code.replace('hpke', '${import_path}')
: \`import * as HPKE from '${import_path}'\n\n\${code}\`;
fs.writeFileSync(\`${base_name}.\${index}.ts\`, content);
});
EOF
rm "${base_name}.tmp"
}
# Process all markdown files in docs
for file in docs/README.md docs/**/*.md; do
extract_typescript_blocks "$file"
done
# Type-check extracted examples and clean up
tsc -p tsconfig.docs.json && rm docs/**/*.ts docs/*.ts