Skip to content

Commit

Permalink
Rewrite images, fixes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
Apostolique committed Dec 16, 2023
1 parent 2881a14 commit 7536391
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/_plugins/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = (md, opts) => {
token.attrSet('target', '_blank')
token.attrSet('rel', 'noopener noreferrer')
} else if (!url.startsWith('#') && !url.startsWith('mailto:')) {
normalizeHref(hrefAttr, env, token, true)
normalizeHref(hrefAttr, env, token, true, false)
}
}

Expand All @@ -35,21 +35,24 @@ module.exports = (md, opts) => {
const url = hrefAttr[1]
const isOutbound = /^(?:[a-z]+:)?\/\//.test(url)
if (!isOutbound) {
normalizeHref(hrefAttr, env, token, false)
normalizeHref(hrefAttr, env, token, false, true)
}
}

return defaultRender(tokens, idx, options, env, self)
}

function normalizeHref(hrefAttr, env, token, addAttr) {
function normalizeHref(hrefAttr, env, token, addAttr, isImage) {
let url = hrefAttr[1]

const parsed = new URL(url, 'http://a.com')
let cleanUrl = url.replace(/\#.*$/, '').replace(/\?.*$/, '')

applyBase = true

// TODO: It would be better if images outside the docs were copied into the docs folder to be hosted with the site.
let repo = isImage ? site.repo.replace('github', 'raw.githubusercontent').replace('/tree/', '/') : site.repo

// Remove markdown extension.
let isMarkdown = /(?:(readme))?.(md)$/i;
if (isMarkdown.test(cleanUrl)) {
Expand Down Expand Up @@ -81,7 +84,7 @@ module.exports = (md, opts) => {
} else if (cleanUrl === '/') {
} else {
// The url points somewhere in the repo outside the pages. Rewrite links to GitHub.
cleanUrl = cleanUrl.replace(/^\//, site.repo)
cleanUrl = cleanUrl.replace(/^\//, repo)
applyBase = false

if (addAttr) {
Expand All @@ -91,7 +94,7 @@ module.exports = (md, opts) => {
}
} else if (!/^\/$/.test(cleanUrl)) { // Exclude '/'
// The url points somewhere in the repo outside the pages. Rewrite links to GitHub.
cleanUrl = cleanUrl.replace(/^\//, site.repo)
cleanUrl = cleanUrl.replace(/^\//, repo)
applyBase = false

if (addAttr) {
Expand Down

0 comments on commit 7536391

Please sign in to comment.