Skip to content

Commit

Permalink
fix: conditional for "reference" links that do not start with a path …
Browse files Browse the repository at this point in the history
…prefix
  • Loading branch information
PatrickHeneise committed Apr 5, 2023
1 parent 4879c3b commit bddd21c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-node-resolve": "^15.0.2",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3",
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

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

18 changes: 16 additions & 2 deletions src/lib/remark-plugins/links/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default function relativeLinks(options) {
extensions = options.extensions
}

// Note: this has gotten incredibly complex over time and could use some refactoring
function visitor(node) {
let nodePrefix = options.prefix
if (node && node.url && !node.url.startsWith('http')) {
Expand Down Expand Up @@ -40,8 +41,17 @@ export default function relativeLinks(options) {
nodePrefix = ''
pathParts = []
} else {
const removeLast = slug.length - depth - 1
pathParts = slug.slice(0, removeLast)
// Special case for links that do not have a path prefix and end with a slash to direct into a README
if (
node.url.match(/^[a-zA-Z]/) &&
node.url.endsWith('/') &&
options.trailingSlash === true
) {
pathParts = slug
} else {
const removeLast = slug.length - depth - 1
pathParts = slug.slice(0, removeLast)
}
}
}

Expand Down Expand Up @@ -90,6 +100,10 @@ export default function relativeLinks(options) {
if (node.url.includes('README')) {
node.url = node.url.replace('README', '')
}

if (node.url.endsWith('//')) {
node.url = node.url.slice(0, -1)
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/remark-plugins/links.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ const cases = [
slug: ['usage', 'gateway'],
options: { trailingSlash: true },
expected: '/docs/usage/ingress/#supported-annotation'
},
{
url: 'base64/',
prefix: 'docs',
slug: ['step-cli', 'reference'],
options: { trailingSlash: true, useMDX: true },
expected: '/docs/step-cli/reference/base64/'
}
]

Expand Down

0 comments on commit bddd21c

Please sign in to comment.