Skip to content

Commit a58e094

Browse files
committed
Relative paths for docs/ issue + edit on github link + path.resolve windows issue
1 parent 99536ec commit a58e094

File tree

8 files changed

+98
-33
lines changed

8 files changed

+98
-33
lines changed

config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const config = {
3030
description:
3131
'Prisma replaces traditional ORMs and can be used to build GraphQL servers, REST APIs, microservices & more.',
3232
keywords: 'Docs, prisma, 2.0',
33-
docsLocation: 'https://github.com/prisma/prisma2-docs/tree/master/content',
33+
docsLocation: 'https://github.com/prisma/docs/tree/master/content',
3434
twitter: {
3535
site: '@prisma',
3636
creator: '@prisma',

gatsby-config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const gatsbyRemarkPlugins = [
1919
{
2020
resolve: `gatsby-remark-images`,
2121
},
22+
{
23+
resolve: require.resolve('./plugins/gatsby-remark-to-absoluteurl'),
24+
},
2225
{
2326
resolve: require.resolve('./plugins/gatsby-remark-check-links-numberless'),
2427
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"gatsby-remark-autolink-headers": "^2.2.1",
3232
"gatsby-remark-images": "^3.2.1",
3333
"gatsby-remark-prismjs": "^3.2.5",
34+
"gatsby-remark-rewrite-relative-links": "^1.0.8",
3435
"gatsby-source-filesystem": "^2.0.24",
3536
"gatsby-source-remote-file": "^0.1.9",
3637
"gatsby-transformer-remark": "^2.3.2",

plugins/gatsby-remark-check-links-numberless/index.js

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ function getCacheKey(node) {
55
return `remark-check-links-${node.id}-${node.internal.contentDigest}`
66
}
77

8-
function getHeadingsMapKey(link, path) {
8+
function getHeadingsMapKey(link, pathUrl) {
99
let key = link
1010
const hashIndex = link.indexOf('#')
1111
const hasHash = hashIndex !== -1
1212
if (hasHash) {
13-
key = link.startsWith('#') ? path : link.slice(0, hashIndex)
13+
key = link.startsWith('#') ? pathUrl : link.slice(0, hashIndex)
1414
}
1515

1616
return {
@@ -32,6 +32,7 @@ module.exports = async function plugin(
3232
{ exceptions = [], ignore = [], verbose = true } = {}
3333
) {
3434
const withPathPrefix = createPathPrefixer(pathPrefix)
35+
const pathSep = path.sep || '/'
3536
if (!markdownNode.fields) {
3637
// let the file pass if it has no fields
3738
return markdownAST
@@ -48,23 +49,6 @@ module.exports = async function plugin(
4849

4950
if (!node.url.startsWith('mailto:') && !/^https?:\/\//.test(node.url)) {
5051
let tranformedUrl = node.url
51-
52-
if (!node.url.startsWith('#') && !node.url.startsWith('/')) {
53-
const parent = getNode(markdownNode.parent)
54-
tranformedUrl = withPathPrefix(
55-
path
56-
.resolve(
57-
markdownNode.fields.slug
58-
.replace(/\d+-/g, '')
59-
.replace(/\/$/, '')
60-
.split(path.sep)
61-
.slice(0, parent.name === 'index' ? undefined : -1)
62-
.join(path.sep) || '/',
63-
node.url
64-
)
65-
.replace(/\/?(\?|#|$)/, '/$1')
66-
)
67-
}
6852
links.push({
6953
...node,
7054
tranformedUrl,
@@ -78,7 +62,7 @@ module.exports = async function plugin(
7862
const parent = await getNode(markdownNode.parent)
7963
const setAt = Date.now()
8064
cache.set(getCacheKey(parent), {
81-
path: withPathPrefix(markdownNode.fields.slug.replace(/\d+-/g, '').concat('/')),
65+
path: withPathPrefix(markdownNode.fields.slug.replace(/\d+-/g, '').concat(pathSep)),
8266
links,
8367
headings,
8468
setAt,
@@ -115,23 +99,23 @@ module.exports = async function plugin(
11599
const prefixedExceptions = exceptions.map(withPathPrefix)
116100
const pathKeys = Object.keys(linksMap)
117101

118-
for (const path in linksMap) {
119-
if (prefixedIgnore.includes(path)) {
102+
for (const pathL in linksMap) {
103+
if (prefixedIgnore.includes(pathL)) {
120104
// don't count broken links for ignored pages
121105
continue
122106
}
123107

124-
const linksForPath = linksMap[path]
108+
const linksForPath = linksMap[pathL]
125109
if (linksForPath.length) {
126110
const brokenLinks = linksForPath.filter(link => {
127111
// return true for broken links, false = pass
128-
const { key, hasHash, hashIndex } = getHeadingsMapKey(link.tranformedUrl, path)
112+
const { key, hasHash, hashIndex } = getHeadingsMapKey(link.tranformedUrl, pathL)
129113
if (prefixedExceptions.includes(key)) {
130114
return false
131115
}
132116

133117
const url = link.tranformedUrl.slice(0, hashIndex)
134-
const urlToCheck = url.slice(-1) === '/' ? url : url.concat('/')
118+
const urlToCheck = url.slice(-1) === pathSep ? url : url.concat(pathSep)
135119
const headings = headingsMap[key]
136120

137121
if (headings) {
@@ -149,7 +133,7 @@ module.exports = async function plugin(
149133
const brokenLinkCount = brokenLinks.length
150134
totalBrokenLinks += brokenLinkCount
151135
if (brokenLinkCount && verbose) {
152-
console.warn(`${brokenLinkCount} broken links found on ${path}`)
136+
console.warn(`${brokenLinkCount} broken links found on ${pathL}`)
153137
for (const link of brokenLinks) {
154138
let prefix = '-'
155139
if (link.position) {
@@ -162,7 +146,7 @@ module.exports = async function plugin(
162146
':'
163147
)
164148
}
165-
console.warn(`${prefix} ${link.url}`)
149+
console.warn(`${prefix} ${link.originalUrl}`)
166150
}
167151
console.log('')
168152
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var visit = require('unist-util-visit')
2+
const path = require('path')
3+
4+
function withPathPrefix(url, pathPrefix) {
5+
const prefixed = pathPrefix + url
6+
return prefixed.replace(/\/\//, '/')
7+
}
8+
9+
module.exports = function plugin({ markdownAST, markdownNode, pathPrefix, getNode }) {
10+
function visitor(node) {
11+
node.originalUrl = node.url
12+
if (
13+
markdownNode.fields &&
14+
markdownNode.fields.slug &&
15+
!node.url.startsWith('/') &&
16+
!node.url.startsWith('#') &&
17+
!node.url.startsWith('mailto:') &&
18+
!/^https?:\/\//.test(node.url)
19+
) {
20+
const parent = getNode(markdownNode.parent)
21+
node.url = withPathPrefix(
22+
path
23+
.resolve(
24+
markdownNode.fields.slug
25+
.replace(/\d+-/g, '')
26+
.replace(/\/$/, '')
27+
.split(path.sep)
28+
.slice(0, parent.name === 'index' ? undefined : -1)
29+
.join(path.sep) || '/',
30+
node.url
31+
)
32+
.replace(/\/?(\?|#|$)/, '/$1'),
33+
pathPrefix
34+
)
35+
}
36+
}
37+
38+
visit(markdownAST, 'link', visitor)
39+
40+
return markdownAST
41+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "gatsby-remark-to-absoluteurl",
3+
"version": "1.0.0",
4+
"description": "Relative to absolute url",
5+
"main": "index.js",
6+
"author": "Nilufar Bava <[email protected]>",
7+
"license": "ISC",
8+
"keywords": [
9+
"gatsby",
10+
"gatsby-plugin"
11+
]
12+
}

src/components/customMdx/button.tsx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import ArrowRight from '../../icons/ArrowRight'
55
import withProps from 'styled-components-ts'
66
import { useLocation } from '@reach/router'
77
import { withPrefix } from 'gatsby'
8+
import isAbsoluteUrl from 'is-absolute-url'
9+
10+
import * as path from 'path'
811

912
export interface ButtonProps {
1013
href?: string
@@ -72,13 +75,27 @@ export const ButtonWrapper = withProps<ButtonProps>(styled.a)`
7275
// background: ${p => darken(0.07, backgroundColorMap[p.color || 'green'])};
7376
// }
7477

75-
const ButtonLink = (props: ButtonProps) => {
78+
const getAbsPath = (href: any, location: any) => {
79+
return withPrefix(
80+
path
81+
.resolve(
82+
location.pathname
83+
.replace('/docs', '')
84+
.replace('docs', '')
85+
.replace(/\/$/, '')
86+
.split(path.sep)
87+
.slice(0, -1)
88+
.join(path.sep) || '/',
89+
href
90+
)
91+
.replace(/\/?(\?|#|$)/, '/$1')
92+
)
93+
}
94+
const ButtonLink = ({ href, ...props }: ButtonProps) => {
7695
const location = useLocation()
77-
if (location.pathname === '/docs/') {
78-
props.href = withPrefix(props.href!)
79-
}
96+
const newHref = isAbsoluteUrl(href) ? href : getAbsPath(href, location)
8097
return (
81-
<ButtonWrapper {...props}>
98+
<ButtonWrapper href={newHref} {...props}>
8299
{props.arrowLeft && <StyledArrowLeft />}
83100
{props.children}
84101
{props.arrow && <StyledArrow />}

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7107,6 +7107,13 @@ gatsby-remark-prismjs@^3.2.5:
71077107
parse-numeric-range "^0.0.2"
71087108
unist-util-visit "^1.4.1"
71097109

7110+
gatsby-remark-rewrite-relative-links@^1.0.8:
7111+
version "1.0.8"
7112+
resolved "https://registry.yarnpkg.com/gatsby-remark-rewrite-relative-links/-/gatsby-remark-rewrite-relative-links-1.0.8.tgz#b2a1190336fa0760518a4e318bc2c503a904f20a"
7113+
integrity sha512-7jCyMM+AWdp8mFLUWuJ5RGPQIKFzpLqf253QR6Aq8xrhlV0Bcz2k1+03MxPnGP0R5XetIoRm2W864KrbIZdk9Q==
7114+
dependencies:
7115+
unist-util-visit "^2.0.0"
7116+
71107117
gatsby-remark-sectionize@^1.0.0:
71117118
version "1.0.0"
71127119
resolved "https://registry.yarnpkg.com/gatsby-remark-sectionize/-/gatsby-remark-sectionize-1.0.0.tgz#3b2c7e407738160fe3dd1d62db955cc64e8ceea4"

0 commit comments

Comments
 (0)