Skip to content

Commit

Permalink
fix(html): image link (neurosnap#7)
Browse files Browse the repository at this point in the history
Use https://raw.githubusercontent.com to display local images from repositories correctly.
  • Loading branch information
luckasRanarison authored Sep 22, 2023
1 parent 433968b commit d004772
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/scripts/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@ async function clean(
const db = JSON.parse(file.toString());
const markdownFile = await Deno.readTextFile(mdFile);
const markdownDb = JSON.parse(markdownFile.toString());
const domain = "https://github.com";
const domainRaw = "https://raw.githubusercontent.com";

const plugins = Object.values(db.plugins) as Plugin[];
plugins.forEach((plugin) => {
console.log(`processing ${plugin.id}`);
marked.use({
walkTokens: (token: any) => {
const domain = "https://github.com";
const pre =
`${domain}/${plugin.username}/${plugin.repo}/blob/${plugin.branch}`;

if (token.type === "link" || token.type === "image") {
if (
token.href &&
!token.href.startsWith("http") &&
!token.href.startsWith("#")
) {
if (
token.href &&
!token.href.startsWith("http") &&
!token.href.startsWith("#")
) {
if (token.type === "link") {
const pre = `${domain}/${plugin.username}/${plugin.repo}/blob/${plugin.branch}`;
token.href = `${pre}/${token.href.replace("./", ``)}`;
} else if (token.type === "image") {
const pre = `${domainRaw}/${plugin.username}/${plugin.repo}/${plugin.branch}`;
token.href = `${pre}/${token.href.replace("./", ``)}`;
}
} else if (token.type === "html") {
Expand Down

0 comments on commit d004772

Please sign in to comment.