-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrunme-badge-plugin.js
53 lines (46 loc) · 1.34 KB
/
runme-badge-plugin.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
const { visit } = require("unist-util-visit");
const path = require("node:path");
const yaml = require("js-yaml");
const runmePlugin = ({ repository }) => {
if (!repository) {
throw new Error("repository is required");
}
const transformer = async (ast, vfile) => {
const markdownPath = vfile.path.replace(`${vfile.cwd}${path.sep}`, "");
let isRunmeFile = false;
visit(ast, "yaml", (node, index, parent) => {
const yml = yaml.load(node.value);
if (yml?.runme) {
isRunmeFile = true;
}
});
if (!isRunmeFile) {
return;
}
visit(ast, "heading", (node, index, parent) => {
parent.children.splice(index + 1, 0, {
type: "paragraph",
children: [
{
type: "link",
url: `https://runme.dev/api/runme?repository=${encodeURIComponent(
repository
)}&fileToOpen=${markdownPath}`,
title: "Open with runme",
children: [
{
type: "image",
title: "Open with runme",
url: "https://badgen.net/badge/Open%20with/Runme/5B3ADF?icon=https://runme.dev/img/logo.svg",
alt: "Runme badge",
},
],
},
],
});
return false;
});
};
return transformer;
};
module.exports = runmePlugin;