Skip to content

Commit dbc0882

Browse files
committed
Make relative links to non-markdown assets to point to raw user content
1 parent 481ddda commit dbc0882

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

compile-markdown.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// SPDX-License-Identifier: MIT
1212

1313
import fs from "fs";
14+
import path from "path";
1415

1516
import * as marked from "marked";
1617
import * as markedGfmHeadingId from "marked-gfm-heading-id";
@@ -74,8 +75,23 @@ let mainHtml = await marked.parse(readFile(input), {
7475
return;
7576
}
7677

77-
// Remove '.md' extension from links.
78-
token.href = token.href.replace(/(\.md$)|(\.md(?=#))/, "");
78+
// Relative link to markdown; remove extension.
79+
if (/(\.md$)|(\.md(?=#))/.test(token.href)) {
80+
token.href = token.href.replace(/(\.md$)|(\.md(?=#))/, "");
81+
return;
82+
}
83+
84+
// Other relative link; point to raw user content.
85+
let dirname = path.dirname(target);
86+
let href =
87+
`https://raw.githubusercontent.com/deimonn/` +
88+
`${repo}/refs/heads/master/${dirname}/${token.href}`
89+
90+
token.type = "html";
91+
token.text = /* HTML */ `
92+
<a href="${href}" target="_blank">${token.text}</a>
93+
`.trim();
94+
7995
return;
8096
}
8197

0 commit comments

Comments
 (0)