-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfetch-readme.js
More file actions
60 lines (52 loc) · 1.88 KB
/
fetch-readme.js
File metadata and controls
60 lines (52 loc) · 1.88 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
56
57
58
59
60
const fetch = require('node-fetch');
const { resolve } = require('path');
const { writeFile } = require('fs/promises');
const url = require('url');
const GITHUB_HOST = 'https://github.com';
const RAW_GITHUB_HOST = 'https://raw.githubusercontent.com';
const mdLinkRegex =
/\[([\w\s\d\-]+)\]\(((?:\/|https?:\/\/|\.)[\w\d\-./?=#]+)\)/g;
/*
* Remote docs are defined in remote-docs.json at the project root.
* After adding a new file to remote-docs.json, you must manually create the
* directory and add an empty .gitkeep file to it so the build will work.
* For example:
* $ cd opensource.contentauth.org/docs
* $ mkdir c2pa_service_example
* $ cd c2pa_service_example
* $ touch .gitkeep
*/
const remoteDocs = require('../remote-docs.json');
const readmes = remoteDocs.sources
.filter((s) => s.dest)
.map(({ repo, path, dest, branch = 'main' }) => ({
repo,
path,
dest: resolve(__dirname, '..', dest),
branch,
}));
function resolveMarkdownLinks(linkBase, content) {
return content.replaceAll(mdLinkRegex, (match, label, href) => {
let revisedUrl = href;
if (!/^https?:\/\//.test(href)) {
revisedUrl = url.resolve(linkBase, href);
}
return `[${label}](${revisedUrl})`;
});
}
async function download() {
for await (const { repo, path, dest, branch = 'main' } of readmes) {
/*
* Pass {branch} var to pull docs from other than main branch.
*/
const src = `${RAW_GITHUB_HOST}/${repo}/${branch}/${path}`;
const linkBase = `${GITHUB_HOST}/${repo}/blob/${branch}/${path}`;
const res = await fetch(src);
const markdown = await res.text();
//const resolvedMarkdown = resolveMarkdownLinks(linkBase, markdown);
//await writeFile(dest, resolvedMarkdown, { encoding: 'utf-8', flag: 'w+' });
await writeFile(dest, markdown, { encoding: 'utf-8', flag: 'w+' });
console.log('Saved %s to %s', src, dest);
}
}
download();