Skip to content

Commit f7c8e56

Browse files
kenrick95timdorr
authored andcommittedApr 9, 2019
Fix website internal links not prepended with public path (#6678)
* Fix website internal links not prepended with basename * Add loader-utils@1.1.0 to package.json
1 parent 992af48 commit f7c8e56

File tree

4 files changed

+37
-48
lines changed

4 files changed

+37
-48
lines changed
 

‎website/package-lock.json

+11-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎website/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"html-webpack-plugin": "^3.2.0",
4747
"isomorphic-fetch": "^2.2.1",
4848
"jsxstyle": "^2.1.1",
49+
"loader-utils": "1.1.0",
4950
"markdown-it": "^7.0.0",
5051
"postcss-loader": "^2.1.5",
5152
"prismjs": "^1.8.4",

‎website/webpack.config.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ module.exports = {
3232
].concat(
3333
process.env.NODE_ENV === "production"
3434
? [
35-
new SWPrecacheWebpackPlugin({
36-
cacheId: "react-router-website",
37-
staticFileGlobsIgnorePatterns: [/\.map$/]
38-
})
39-
]
35+
new SWPrecacheWebpackPlugin({
36+
cacheId: "react-router-website",
37+
staticFileGlobsIgnorePatterns: [/\.map$/]
38+
})
39+
]
4040
: []
4141
),
4242

@@ -112,7 +112,10 @@ module.exports = {
112112
},
113113
{
114114
test: /\.md(\?(.+))?$/,
115-
loader: "markdown-loader"
115+
loader: "markdown-loader",
116+
options: {
117+
basename: process.env.NODE_ENV === "production" ? "/react-router" : undefined
118+
}
116119
},
117120
{
118121
test: /\.(gif|jpe?g|png|ico)$/,
@@ -132,6 +135,7 @@ module.exports = {
132135
historyApiFallback: true,
133136
quiet: false,
134137
noInfo: false,
138+
publicPath: "/",
135139
stats: {
136140
assets: true,
137141
version: false,

‎website/webpack/markdown-loader.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const cheerio = require("cheerio");
1414
const path = require("path");
1515
const slug = require("slug");
1616
const resolve = require("resolve-pathname");
17+
const loaderUtils = require("loader-utils");
1718

1819
const routerDelegationClassName = "internal-link";
1920

@@ -70,53 +71,53 @@ const isOtherTypeSibling = href =>
7071

7172
const isCrossPackage = href => href.startsWith("../../../");
7273

73-
const makeHref = (hash, ...segments) => {
74-
let href = "/" + segments.join("/").replace(/\.md$/, "");
74+
const makeHref = (basename, hash, ...segments) => {
75+
let href = basename + "/" + segments.join("/").replace(/\.md$/, "");
7576
if (hash) href += "/" + hash;
7677
return href;
7778
};
7879

79-
const correctLinks = ($, moduleSlug, environment, type) => {
80+
const correctLinks = ($, moduleSlug, environment, type, basename) => {
8081
$("a[href]").each((i, e) => {
8182
const $e = $(e);
8283
const [href, hash] = $e.attr("href").split("#");
8384

8485
if (isSelfHeader(href)) {
8586
// #render-func
8687
// /web/api/Route/render-func
87-
$e.attr("href", `/${environment}/${type}/${moduleSlug}/${hash}`);
88+
$e.attr("href", `${basename}/${environment}/${type}/${moduleSlug}/${hash}`);
8889
$e.addClass(routerDelegationClassName);
8990
} else if (isSibling(href)) {
9091
// ./quick-start.md
9192
// /web/guides/quick-start
9293
const [_, doc] = href.split("/");
9394

94-
$e.attr("href", makeHref(hash, environment, type, doc));
95+
$e.attr("href", makeHref(basename, hash, environment, type, doc));
9596
$e.addClass(routerDelegationClassName);
9697
} else if (isOtherTypeSibling(href)) {
9798
// ../api/NativeRouter.md
9899
// /web/api/NativeRouter
99100
const [_, type, doc] = href.split("/");
100-
$e.attr("href", makeHref(hash, environment, type, doc));
101+
$e.attr("href", makeHref(basename, hash, environment, type, doc));
101102
$e.addClass(routerDelegationClassName);
102103
} else if (isCrossPackage(href)) {
103104
// ../../../react-router/docs/api/Route.md
104105
// /core/api/Route
105106
const [$0, $1, $2, env, $4, type, doc] = href.split("/");
106-
$e.attr("href", makeHref(hash, envMap[env], type, doc));
107+
$e.attr("href", makeHref(basename, hash, envMap[env], type, doc));
107108
$e.addClass(routerDelegationClassName);
108109
}
109110
});
110111
};
111112

112-
const makeHeaderLinks = ($, moduleSlug, environment, type) => {
113+
const makeHeaderLinks = ($, moduleSlug, environment, type, basename) => {
113114
// can abstract these two things a bit, but it's late.
114115
$("h1").each((i, e) => {
115116
const $e = $(e);
116117
$e.attr("id", moduleSlug);
117118
const children = $e.html();
118119
const link = $(
119-
`<a href="/${environment}/${type}/${moduleSlug}" class="${routerDelegationClassName}"/>`
120+
`<a href="${basename}/${environment}/${type}/${moduleSlug}" class="${routerDelegationClassName}"/>`
120121
);
121122
link.html(children);
122123
$e.empty().append(link);
@@ -129,7 +130,7 @@ const makeHeaderLinks = ($, moduleSlug, environment, type) => {
129130
$e.attr("id", `${moduleSlug}-${slug}`);
130131
const children = $e.html();
131132
const link = $(
132-
`<a href="/${environment}/${type}/${moduleSlug}/${slug}" class="${routerDelegationClassName}"/>`
133+
`<a href="${basename}/${environment}/${type}/${moduleSlug}/${slug}" class="${routerDelegationClassName}"/>`
133134
);
134135
link.html(children);
135136
$e.empty().append(link);
@@ -145,11 +146,13 @@ const md = markdownIt({
145146

146147
module.exports = function(content) {
147148
this.cacheable();
149+
const options = loaderUtils.getOptions(this);
150+
const basename = options.basename || "";
148151
const markup = md.render(content);
149152
const $markup = cheerio.load(markup);
150153
const title = extractHeaders($markup, "h1", this.data.type)[0];
151-
correctLinks($markup, title.slug, this.data.environment, this.data.type);
152-
makeHeaderLinks($markup, title.slug, this.data.environment, this.data.type);
154+
correctLinks($markup, title.slug, this.data.environment, this.data.type, basename);
155+
makeHeaderLinks($markup, title.slug, this.data.environment, this.data.type, basename);
153156
const headers = extractHeaders($markup, "h2", this.data.type);
154157
const value = {
155158
markup: $markup.html(),

0 commit comments

Comments
 (0)
Please sign in to comment.