Skip to content

Commit

Permalink
added pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Jul 12, 2024
1 parent 3379d80 commit fbcc0a6
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"lodash-es": "^4.17.21",
"lowlight": "^3.1.0",
"mdast-util-find-and-replace": "^3.0.1",
"mdast-util-to-string": "^4.0.0",
"node-html-parser": "^6.1.13",
"purgecss-webpack-plugin": "^6.0.0",
"rehype-highlight": "^7.0.0",
Expand Down
3 changes: 2 additions & 1 deletion src/views/develop/guides/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ playback.
low-level media APIs like `libndl-media`,
it provides low-latency game streaming from a PC. It also uses SDL2 for rendering and input handling.

Next: [Environment Setup](/develop/guides/env-setup)
* Next
* [Environment Setup](/develop/guides/env-setup)
4 changes: 3 additions & 1 deletion webpack/markdown-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import remarkGemoji from 'remark-gemoji';
import remarkRehype from 'remark-rehype';
import remarkTabbedCodeBlock from "./remark/tabbed-code-block.js";
import remarkImageClass from "./remark/image-class.js";
import remarkPagination from "./remark/pagination.js";

import rehypeRaw from 'rehype-raw';
import rehypeSlug from 'rehype-slug';
Expand Down Expand Up @@ -124,11 +125,12 @@ function alertRestyle() {
const parser = remark()
.use(remarkGfm)
.use([remarkAlert, alertRestyle])
.use(remarkSectionize)
.use(remarkBootstrapIcon)
.use(remarkGemoji)
.use(remarkTabbedCodeBlock)
.use(remarkImageClass, {class: 'img-fluid rounded-3'})
.use(remarkPagination)
.use(remarkSectionize)
.use(remarkRehype, {allowDangerousHtml: true})
.use(rehypeRaw)
.use(rehypeSlug)
Expand Down
64 changes: 64 additions & 0 deletions webpack/remark/pagination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {toString as mdToString} from 'mdast-util-to-string';
import {groupBy, mapValues} from "lodash-es";
import {visit} from "unist-util-visit";

import {html} from '../htm-rehype.js';
import {toHtml} from "hast-util-to-html";

/**
* @param node {MarkdownNode}
* @returns {MarkdownLink[]}
*/
function extractLinks(node) {
const result = [];
if (node) visit(node, 'link', (link) => {
result.push(link);
});
return result;
}

/**
* @return {Processor}
*/
export default function () {
return (tree) => {
/** @type {MarkdownList} */
const lastChild = tree.children[tree.children.length - 1];
if (lastChild?.type !== 'list') {
return;
}
/** @type {Record<string, MarkdownLink[]>} */
const entries = mapValues(groupBy(lastChild.children, i => mdToString(i.children[0])),
l => l.flatMap(i => extractLinks(i.children[1])));
const prev = entries['Previous'];
const next = entries['Next'];
if (!prev && !next) {
return;
}
tree.children[tree.children.length - 1] = {
type: 'html',
value: toHtml(html`
<div class="row mt-5">
<div class="col-md-6">
${prev && html`
<div class="card px-3 py-2 align-items-start">
<small><i class="bi bi-chevron-double-left small"></i> Previous</small>
${prev.map(link => html`
<a href="${link.url}" class="d-block text-decoration-none">${mdToString(link)}</a>
`)}
</div>`}
</div>
<div class="col-md-6">
${next && html`
<div class="card px-3 py-2 align-items-end">
<small>Next <i class="bi bi-chevron-double-right small"></i></small>
${next.map(link => html`
<a href="${link.url}" class="d-block text-decoration-none">${mdToString(link)}</a>
`)}
</div>`}
</div>
</div>
`)
}
}
}
2 changes: 2 additions & 0 deletions webpack/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/** @typedef {import('mdast').Root} MarkdownRoot */
/** @typedef {import('mdast').Node} MarkdownNode */
/** @typedef {import('mdast').List} MarkdownList */
/** @typedef {import('mdast').MarkdownLink} MarkdownLink */
/** @typedef {import('mdast').Image} MarkdownImage */

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

0 comments on commit fbcc0a6

Please sign in to comment.