Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(MarkdownParser): refine content path in anchor link #1629

Merged
merged 3 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ export default defineNuxtConfig({
}
},
content: {
sources: [
{
sources: {
v1: {
name: 'v1',
prefix: '/v1',
driver: 'fs',
base: resolve(__dirname, 'content-v1/en')
},
{
'v1-ja': {
name: 'v1-ja',
prefix: '/ja/v1',
driver: 'fs',
base: resolve(__dirname, 'content-v1/ja')
},
{
'v1-fr': {
name: 'v1-fr',
prefix: '/fr/v1',
driver: 'fs',
base: resolve(__dirname, 'content-v1/fr')
},
{
'v1-ru': {
name: 'v1-ru',
prefix: '/ru/v1',
driver: 'fs',
base: resolve(__dirname, 'content-v1/ru')
}
],
},
highlight: {
preload: ['xml']
}
Expand Down
8 changes: 3 additions & 5 deletions src/runtime/markdown-parser/handler/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { all } from 'mdast-util-to-hast'
import { encode } from 'mdurl'
import type { MdastNode } from 'mdast-util-to-hast/lib'
import { isRelative } from 'ufo'
import { generatePath } from '../../transformers/path-meta'

type Node = MdastNode & {
title: string
Expand All @@ -28,11 +29,8 @@ export default function link (h: H, node: Node) {
}

function normalizeLink (link: string) {
if (isRelative(link) || (!/^https?/.test(link) && !link.startsWith('/'))) {
return link.split('/')
.map(x => x.replace(/^[0-9]*\./g, ''))
.join('/')
.replace(/\.md$/g, '')
if (link.endsWith('.md') && (isRelative(link) || (!/^https?/.test(link) && !link.startsWith('/')))) {
return generatePath(link.replace(/\.md$/, ''), { forceLeadingSlash: false })
} else {
return link
}
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/transformers/path-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ const isPartial = (path: string): boolean => path.split(/[:/]/).some(part => par
* @param path file full path
* @returns generated slug
*/
const generatePath = (path: string): string =>
withLeadingSlash(withoutTrailingSlash(path.split('/').map(part => slugify(refineUrlPart(part), { lower: true })).join('/')))
export const generatePath = (path: string, { forceLeadingSlash = true } = {}): string => {
path = path.split('/').map(part => slugify(refineUrlPart(part), { lower: true })).join('/')
return forceLeadingSlash ? withLeadingSlash(withoutTrailingSlash(path)) : path
}

/**
* generate title from file path
Expand Down
37 changes: 37 additions & 0 deletions test/features/parser-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,42 @@ export const testMarkdownParser = () => {
expect(parsed.body.children[1].tag).toEqual('p')
expect(parsed.body.children[1].children[1].props.class).toEqual('font-bold text-green')
})

test('handle markdown file path as link', async () => {
const parsed = await $fetch('/api/parse', {
method: 'POST',
body: {
id: 'content:index.md',
content: [
'[link1](3.x)',
'[link1](./3.x)',
'[link1](foo)',
'[link1](foo.md)',
'[link1](01.foo.md)',
'[link1](./01.foo.md)',
'[link1](./../01.foo.md)',
'[link1](../01.foo.md)',
'[link1](../../01.foo.md)',
'[link1](../../01.foo#bar.md)',
'[link1](../../01.foo.draft.md)',
'[link1](../../_foo.draft.md)'
].join('\n')
}
})

const nodes = parsed.body.children[0].children
expect(nodes.shift().props.href).toEqual('3.x')
expect(nodes.shift().props.href).toEqual('./3.x')
expect(nodes.shift().props.href).toEqual('foo')
expect(nodes.shift().props.href).toEqual('foo')
expect(nodes.shift().props.href).toEqual('foo')
expect(nodes.shift().props.href).toEqual('./foo')
expect(nodes.shift().props.href).toEqual('./../foo')
expect(nodes.shift().props.href).toEqual('../foo')
expect(nodes.shift().props.href).toEqual('../../foo')
expect(nodes.shift().props.href).toEqual('../../foobar')
expect(nodes.shift().props.href).toEqual('../../foo')
expect(nodes.shift().props.href).toEqual('../../_foo')
})
})
}