forked from dfinity/portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhat-is-the-ic-cards.js
125 lines (109 loc) · 3.24 KB
/
what-is-the-ic-cards.js
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
const fs = require("fs");
const path = require("path");
const marked = require("marked");
const matter = require("gray-matter");
const logger = require("@docusaurus/logger");
const { isLinkExternal } = require("./utils/links");
const renderer = new marked.Renderer();
const linkRenderer = renderer.link;
renderer.link = (href, title, text) => {
let html = linkRenderer.call(renderer, href, title, text);
if (isLinkExternal(href)) {
// this is an external link, add target="_blank"
html = html.replace(
/^<a /,
`<a target="_blank" rel="noreferrer noopener" `
);
}
if (href.startsWith("https://www.youtube.com/") && text.startsWith("<img ")) {
// this is a youtube thumbnail, add class name
html = html.replace(/^<a /, `<a class="markdown-youtube-thumbnail" `);
}
return html;
};
function getItems(baseDir) {
if (!fs.existsSync(baseDir)) {
return [];
}
const files = fs
.readdirSync(baseDir, {
withFileTypes: true,
})
.filter((d) => d.isFile() && d.name.endsWith(".md"));
return files.map((file) => {
const meta = matter(
fs.readFileSync(path.resolve(baseDir, file.name), { encoding: "utf-8" })
);
return {
name: meta.data.title,
description: marked.parse(meta.content),
links: Object.keys(meta.data.links)
.filter((title) => !!meta.data.links[title])
.map((title) => ({
text: title,
url: meta.data.links[title],
})),
is_community: meta.data.is_community,
in_beta: meta.data.in_beta,
eta: meta.data.eta,
};
});
}
let cache;
/** @type {import('@docusaurus/types').PluginModule} */
const whatIsIcpDataPlugin = async function () {
return {
name: "what-is-the-ic-data",
async loadContent() {
if (!cache) {
const domains = [];
const dirs = fs
.readdirSync(path.resolve(__dirname, "..", "what-is-the-ic"), {
withFileTypes: true,
})
.filter((d) => d.isDirectory());
for (const dir of dirs) {
const indexPath = path.resolve(
__dirname,
"..",
"what-is-the-ic",
dir.name,
"index.md"
);
if (!fs.existsSync(indexPath)) {
logger.warn(
`Warning: no index.md file for what-is-the-ic topic "${dir.name}"`
);
continue;
}
const meta = matter(
fs.readFileSync(indexPath, { encoding: "utf-8" })
);
const baseDir = path.resolve(
__dirname,
"..",
"what-is-the-ic",
dir.name
);
domains.push({
name: meta.data.title,
description: marked.parse(meta.content, { renderer }),
cardImageFit: meta.data.cardImageFit,
image: {
card: meta.data.card,
overlay: meta.data.overlay,
},
});
}
cache = domains;
}
return cache;
},
async contentLoaded({ content, actions }) {
const { createData } = actions;
createData("what-is-the-ic.json", JSON.stringify(content, null, 2));
},
};
};
module.exports = whatIsIcpDataPlugin;
// RoadmapDataPlugin().then((x) => x.loadContent());