Skip to content

Commit ae090be

Browse files
committed
fix: links
1 parent 507e35d commit ae090be

3 files changed

Lines changed: 34 additions & 17 deletions

File tree

docs/usecase/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Another specific link in the page [here with .md](example.md#specific-section)
1010
Another specific link in the page [here with query param](example?id=specific-section)
1111
Another specific link in the page [here with .md and query param](example.md?id=specific-section)
1212

13+
-> This is also [cool](/usecase/cool)
14+
-> A [link with a slash](/usecase/example.md)
15+
-> [README.md](/README.md)
16+
1317
| Syntax | Description |
1418
| --------- | ----------- |
1519
| Header | Title |

docs/usecase/example.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,3 @@ uhhhh up.svg (up.svg not replaced!) img.png 🐱
2121
# Specific Section
2222

2323
Hello
24-
25-
# Another Section
26-
27-
World

src/process-inner-links.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,39 @@ module.exports = ({ content, name }, _, arr) => {
4545
anchor = params.get('id')
4646
}
4747
}
48-
const resolvedPath = path.resolve(path.dirname(name), linkPath)
48+
4949
const docsPath = arr.find(({ name }) => name.endsWith('docs/README.md'))
5050
const docsDir = docsPath ? path.dirname(docsPath.name) : ''
5151

52-
return {
53-
file: arr.find(({ name: fileName }) => {
54-
if (fileName === resolvedPath) return true
55-
if (fileName === `${resolvedPath}.md`) return true
56-
57-
if (docsDir) {
58-
const resolvedFromDocs = path.resolve(docsDir, linkPath)
59-
if (fileName === resolvedFromDocs) return true
60-
if (fileName === `${resolvedFromDocs}.md`) return true
61-
}
52+
let file
53+
54+
const findFile = (basePath) => {
55+
return arr.find(({ name: fileName }) => {
56+
const absoluteFileName = path.resolve(process.cwd(), fileName)
57+
if (absoluteFileName === basePath) return true
58+
if (absoluteFileName === `${basePath}.md`) return true
59+
if (absoluteFileName === path.join(basePath, 'README.md')) return true
6260
return false
63-
}),
61+
})
62+
}
63+
64+
if (linkPath.startsWith('/')) {
65+
const absolutePath = path.join(docsDir, linkPath)
66+
file = findFile(absolutePath)
67+
} else {
68+
// First, try to resolve the path relative to the current file's directory.
69+
const resolvedPath = path.resolve(path.dirname(name), linkPath)
70+
file = findFile(resolvedPath)
71+
72+
// If the file is not found, try to resolve it relative to the docs root directory as a fallback.
73+
if (!file && docsDir) {
74+
const resolvedFromDocs = path.resolve(docsDir, linkPath)
75+
file = findFile(resolvedFromDocs)
76+
}
77+
}
78+
79+
return {
80+
file,
6481
link: href,
6582
anchor
6683
}
@@ -91,7 +108,7 @@ module.exports = ({ content, name }, _, arr) => {
91108
}
92109

93110
if (!headingNode) {
94-
console.error('no heading (non blocking error)', link)
111+
console.warn(`no heading found for ${link}. The link will point to the top of the page.`)
95112
return { link, unsafeTag: '' }
96113
}
97114

0 commit comments

Comments
 (0)