Skip to content

Commit 33c62d9

Browse files
authored
Return bad content message locally. (#2237)
* Separate book page title plugin. * Add basic test for book title. * Don't set title on pages without content. * Correctly avoid loading index.html for md
1 parent 61ba9d2 commit 33c62d9

File tree

7 files changed

+105
-205
lines changed

7 files changed

+105
-205
lines changed

site/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<script src="/static/js/config.js"></script>
3333
<script src="/static/js/plugins/plugins.js"></script>
3434
<script src="/static/js/plugins/copy_buttons.js"></script>
35+
<script src="/static/js/plugins/book_page_title.js"></script>
3536
<script src="//cdn.jsdelivr.net/npm/docsify/lib/docsify.min.js"></script>
3637
<script src="https://cdn.jsdelivr.net/npm/docsify-themeable@0/dist/js/docsify-themeable.min.js"></script>
3738
<script src="//cdn.jsdelivr.net/npm/docsify-pagination/dist/docsify-pagination.min.js"></script>

site/package-lock.json

Lines changed: 13 additions & 178 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"devDependencies": {
99
"@babel/plugin-proposal-optional-chaining": "^7.14.2",
10+
"@gouch/to-title-case": "^2.2.1",
1011
"autoprefixer": "^9.7.4",
1112
"href-checker": "etefera/href-checker#docsify",
1213
"jest": "^27.0.4",

site/site_check.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ server {
33
server_name localhost;
44
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
55

6-
location ~* README.md$ {
6+
location ~* \.md$ {
77
root /usr/share/nginx/html;
88
try_files $uri =404;
99
}
1010

11-
location / {
11+
location ~* ^[\/\w\.-]+[^\.]?.?.?$ {
1212
root /usr/share/nginx/html;
1313
index index.html index.htm;
1414
try_files $uri /index.html;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Add title to book Markdown pages based on directory structure.
2+
function processBookPageTitle(content) {
3+
const pathname = window.location.pathname.toLowerCase();
4+
5+
const bookPathMatch = pathname.match(/^\/book\/(\d+)-(.+)\/(\d+)?-?(.+)?/);
6+
7+
if (content && !content.startsWith("<!DOCTYPE html>") && bookPathMatch) {
8+
const pageNumber = parseInt(bookPathMatch[3]);
9+
10+
// Use chapter name if on intro page and page name otherwise.
11+
const chapterNum = `# ${parseInt(bookPathMatch[1])}${
12+
pageNumber > 0 ? `.${pageNumber}` : ""
13+
}`;
14+
const pageTitle = pageNumber > 0 ? bookPathMatch[4] : bookPathMatch[2];
15+
16+
content =
17+
`${chapterNum} ${pageTitle.replace(/-/g, " ").toTitleCase()}\n` +
18+
content;
19+
}
20+
21+
return content;
22+
}
23+
24+
// Load plugins into Docsify.
25+
window.$docsify = window.$docsify || {};
26+
window.$docsify.plugins = [
27+
(hook) => hook.beforeEach(processBookPageTitle),
28+
].concat(window.$docsify.plugins || []);
29+
30+
// Export functions for testing.
31+
if (typeof module !== "undefined") {
32+
module.exports = { processBookPageTitle };
33+
}

0 commit comments

Comments
 (0)