Skip to content

Commit fbcc0a6

Browse files
committed
added pagination
1 parent 3379d80 commit fbcc0a6

File tree

6 files changed

+73
-2
lines changed

6 files changed

+73
-2
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"lodash-es": "^4.17.21",
4242
"lowlight": "^3.1.0",
4343
"mdast-util-find-and-replace": "^3.0.1",
44+
"mdast-util-to-string": "^4.0.0",
4445
"node-html-parser": "^6.1.13",
4546
"purgecss-webpack-plugin": "^6.0.0",
4647
"rehype-highlight": "^7.0.0",

src/views/develop/guides/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ playback.
2828
low-level media APIs like `libndl-media`,
2929
it provides low-latency game streaming from a PC. It also uses SDL2 for rendering and input handling.
3030

31-
Next: [Environment Setup](/develop/guides/env-setup)
31+
* Next
32+
* [Environment Setup](/develop/guides/env-setup)

webpack/markdown-loader.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import remarkGemoji from 'remark-gemoji';
66
import remarkRehype from 'remark-rehype';
77
import remarkTabbedCodeBlock from "./remark/tabbed-code-block.js";
88
import remarkImageClass from "./remark/image-class.js";
9+
import remarkPagination from "./remark/pagination.js";
910

1011
import rehypeRaw from 'rehype-raw';
1112
import rehypeSlug from 'rehype-slug';
@@ -124,11 +125,12 @@ function alertRestyle() {
124125
const parser = remark()
125126
.use(remarkGfm)
126127
.use([remarkAlert, alertRestyle])
127-
.use(remarkSectionize)
128128
.use(remarkBootstrapIcon)
129129
.use(remarkGemoji)
130130
.use(remarkTabbedCodeBlock)
131131
.use(remarkImageClass, {class: 'img-fluid rounded-3'})
132+
.use(remarkPagination)
133+
.use(remarkSectionize)
132134
.use(remarkRehype, {allowDangerousHtml: true})
133135
.use(rehypeRaw)
134136
.use(rehypeSlug)

webpack/remark/pagination.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {toString as mdToString} from 'mdast-util-to-string';
2+
import {groupBy, mapValues} from "lodash-es";
3+
import {visit} from "unist-util-visit";
4+
5+
import {html} from '../htm-rehype.js';
6+
import {toHtml} from "hast-util-to-html";
7+
8+
/**
9+
* @param node {MarkdownNode}
10+
* @returns {MarkdownLink[]}
11+
*/
12+
function extractLinks(node) {
13+
const result = [];
14+
if (node) visit(node, 'link', (link) => {
15+
result.push(link);
16+
});
17+
return result;
18+
}
19+
20+
/**
21+
* @return {Processor}
22+
*/
23+
export default function () {
24+
return (tree) => {
25+
/** @type {MarkdownList} */
26+
const lastChild = tree.children[tree.children.length - 1];
27+
if (lastChild?.type !== 'list') {
28+
return;
29+
}
30+
/** @type {Record<string, MarkdownLink[]>} */
31+
const entries = mapValues(groupBy(lastChild.children, i => mdToString(i.children[0])),
32+
l => l.flatMap(i => extractLinks(i.children[1])));
33+
const prev = entries['Previous'];
34+
const next = entries['Next'];
35+
if (!prev && !next) {
36+
return;
37+
}
38+
tree.children[tree.children.length - 1] = {
39+
type: 'html',
40+
value: toHtml(html`
41+
<div class="row mt-5">
42+
<div class="col-md-6">
43+
${prev && html`
44+
<div class="card px-3 py-2 align-items-start">
45+
<small><i class="bi bi-chevron-double-left small"></i> Previous</small>
46+
${prev.map(link => html`
47+
<a href="${link.url}" class="d-block text-decoration-none">${mdToString(link)}</a>
48+
`)}
49+
</div>`}
50+
</div>
51+
<div class="col-md-6">
52+
${next && html`
53+
<div class="card px-3 py-2 align-items-end">
54+
<small>Next <i class="bi bi-chevron-double-right small"></i></small>
55+
${next.map(link => html`
56+
<a href="${link.url}" class="d-block text-decoration-none">${mdToString(link)}</a>
57+
`)}
58+
</div>`}
59+
</div>
60+
</div>
61+
`)
62+
}
63+
}
64+
}

webpack/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
/** @typedef {import('mdast').Root} MarkdownRoot */
99
/** @typedef {import('mdast').Node} MarkdownNode */
10+
/** @typedef {import('mdast').List} MarkdownList */
11+
/** @typedef {import('mdast').MarkdownLink} MarkdownLink */
1012
/** @typedef {import('mdast').Image} MarkdownImage */
1113

1214
/** @typedef {import('mdast').Code} Code */

0 commit comments

Comments
 (0)