Skip to content

Commit

Permalink
Update markdown plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jlpereira committed Oct 2, 2023
1 parent d913e4e commit 6a7ffc4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
35 changes: 31 additions & 4 deletions src/plugins/markdown/relativeToRouter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
export function relativeToRouterPlugin(md) {
export function relativeToRouterPlugin(md, configuration) {
const isStaticAsset = (url) => {
const regex = /\.\w+$/

return regex.test(url)
}

const makeUrlToStaticAsset = (url) => {
const baseUrl = configuration.base_url || ''

return (baseUrl + url).replaceAll('//', '/')

if (!url.startsWith('/')) {
return (baseUrl + url).replaceAll('//', '/')
} else {
return url
}
}

const scan = (state) => {
state.tokens.forEach((tokens) => {
if (tokens.type !== 'inline') {
Expand All @@ -13,14 +31,23 @@ export function relativeToRouterPlugin(md) {
} else if (inlineTokens[i].type === 'link_open') {
const attrs = inlineTokens[i].attrs
const href = attrs?.find((v) => v[0] === 'href')

if (
href &&
!href[1].startsWith('http') &&
!href[1].startsWith('mailto')
) {
inlineTokens[i].tag = 'router-link'
inlineTokens[i].attrs = [['to', href[1]]]
isRT = true
if (isStaticAsset(href[1])) {
inlineTokens[i].attrs.push([
'href',
makeUrlToStaticAsset(href[1])
])
inlineTokens[i].attrs.push(['target', '_blank'])
} else {
inlineTokens[i].tag = 'router-link'
inlineTokens[i].attrs = [['to', href[1]]]
isRT = true
}
} else {
inlineTokens[i].attrs.push(['rel', 'noopener noreferrer'])
inlineTokens[i].attrs.push(['target', '_blank'])
Expand Down
4 changes: 2 additions & 2 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export default () => {
md.use(variableReplacementPlugin, {
variables: { ...configuration }
})
},
markdownItUses: [relativeToRouterPlugin]
md.use(relativeToRouterPlugin, configuration)
}
}),

Pages({
Expand Down

0 comments on commit 6a7ffc4

Please sign in to comment.