Skip to content

Commit

Permalink
Merge pull request #118 from SpeciesFileGroup/development
Browse files Browse the repository at this point in the history
Add markdown anchor and convert a tag to vue router link
  • Loading branch information
José Luis Pereira authored and GitHub committed Aug 14, 2023
2 parents d42682d + 2fb8fab commit 269f0da
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
26 changes: 14 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"axios": "^1.4.0",
"js-yaml": "^4.1.0",
"leaflet": "^1.9.4",
"markdown-it-anchor": "^8.6.7",
"pinia": "^2.1.6",
"unhead": "^1.2.2",
"vue": "^3.3.4",
Expand Down
29 changes: 29 additions & 0 deletions src/plugins/markdown/relativeToRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export default (md) => {
const scan = (state) => {
state.tokens.forEach((tokens) => {
if (tokens.type !== 'inline') {
return
}
const inlineTokens = tokens.children
let isRT = false
for (let i = 0; i < inlineTokens.length; i++) {
if (isRT && inlineTokens[i].type === 'link_close') {
inlineTokens[i].tag = 'router-link'
isRT = false
} 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')) {
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'])
}
}
}
})
}
md.core.ruler.push('router-link', scan)
}
7 changes: 6 additions & 1 deletion src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ function getHistory() {
export function createRouter() {
return _createRouter({
history: getHistory(),
routes
routes,
scrollBehavior(to, from, savedPosition) {
if (to.hash) {
return { el: to.hash }
}
}
})
}
8 changes: 7 additions & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { loadConfiguration } from './src/utils/loadConfiguration.js'
import path from 'path'
import Vue from '@vitejs/plugin-vue'
import Markdown from 'vite-plugin-md'
import markdownAnchor from 'markdown-it-anchor'
import markdownRelativeToRouter from './src/plugins/markdown/relativeToRouter.js'
import Pages from 'vite-plugin-pages'
import './src/utils/globalVars'

Expand All @@ -26,7 +28,11 @@ export default () => {
}),
Markdown({
wrapperClasses:
'!container mx-auto p-4 sm:pl-0 sm:pr-0 prose dark:prose-invert box-border'
'!container mx-auto p-4 sm:pl-0 sm:pr-0 prose dark:prose-invert box-border',
markdownItSetup(md) {
md.use(markdownAnchor)
},
markdownItUses: [markdownRelativeToRouter]
}),
Pages({
dirs: 'pages',
Expand Down

0 comments on commit 269f0da

Please sign in to comment.