repos / neovimcraft

website that makes it easy to find neovim plugins
git clone https://github.com/neurosnap/neovimcraft.git

neovimcraft / src / scripts
Eric Bower · 11 Apr 23

html.ts

 1import { marked } from "../deps.ts";
 2import type { Plugin } from "../types.ts";
 3
 4await init().catch(console.error);
 5
 6async function init() {
 7  const db = {};
 8  await clean({
 9    htmlDb: db,
10    dbFile: "./data/db.json",
11    mdFile: "./data/markdown.json",
12  });
13  await clean({
14    htmlDb: db,
15    dbFile: "./data/db-config.json",
16    mdFile: "./data/markdown-config.json",
17  });
18  await save(db);
19}
20
21async function clean(
22  { htmlDb, dbFile, mdFile }: {
23    htmlDb: { [key: string]: string };
24    dbFile: string;
25    mdFile: string;
26  },
27) {
28  const file = await Deno.readTextFile(dbFile);
29  const db = JSON.parse(file.toString());
30  const markdownFile = await Deno.readTextFile(mdFile);
31  const markdownDb = JSON.parse(markdownFile.toString());
32
33  const plugins = Object.values(db.plugins) as Plugin[];
34  plugins.forEach((plugin) => {
35    console.log(`processing ${plugin.id}`);
36    marked.use({
37      walkTokens: (token: any) => {
38        const domain = "https://github.com";
39        const pre =
40          `${domain}/${plugin.username}/${plugin.repo}/blob/${plugin.branch}`;
41
42        if (token.type === "link" || token.type === "image") {
43          if (
44            token.href &&
45            !token.href.startsWith("http") &&
46            !token.href.startsWith("#")
47          ) {
48            token.href = `${pre}/${token.href.replace("./", ``)}`;
49          }
50        } else if (token.type === "html") {
51          token.text = "";
52        }
53      },
54    });
55
56    const markdown = markdownDb.markdown[plugin.id];
57    if (!markdown) return;
58    const html = marked(markdown);
59    htmlDb[plugin.id] = html;
60  });
61}
62
63async function save(nextDb: { [key: string]: string }) {
64  try {
65    const json = JSON.stringify({ html: nextDb }, null, 2);
66    await Deno.writeTextFile("./data/html.json", json);
67  } catch (err) {
68    console.error(err);
69  }
70}