Gathering detailed insights and metrics for next-mdx-books
Gathering detailed insights and metrics for next-mdx-books
Gathering detailed insights and metrics for next-mdx-books
Gathering detailed insights and metrics for next-mdx-books
npm install next-mdx-books
Typescript
Module System
Min. Node Version
TypeScript (91.89%)
JavaScript (5.92%)
HTML (2.18%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
CC0-1.0 License
10 Stars
78 Commits
1 Forks
1 Watchers
2 Branches
1 Contributors
Updated on Nov 08, 2024
Latest Version
0.3.2
Package Id
next-mdx-books@0.3.2
Unpacked Size
291.24 kB
Size
79.59 kB
File Count
29
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
13
1
This documentation is best viewed on the website for the docs: https://www.openedtech.dev/Open-EdTech/next-mdx-books/about.md
This package generates/consumes a tree where nodes contain urls that serve markdown/MDX files. We refer to this as the URL tree.
The URL tree can be generated from a table of contents written in markdown, compatible with existing GitBooks. Utility functions are provided to reformat the URL tree to play well with Next.js functions getStaticPaths and getStaticProps so that you can serve all the files specified in your table of contents.
yarn add next-mdx-boooks
#or
npm install next-mdx-books
The summariesToTrees
method takes an array of SUMMARY.md urls and writes to an array of URL trees as bookConfig.json
. Run node script.js
to generate bookConfig.json
.
1var {summariesToTrees} = require("next-mdx-books"); 2 3(async () => { 4 await summariesToTrees( 5 [ 6 { 7 url: 8 "https://github.com/Open-EdTech/mostly-adequate-guide/blob/master/SUMMARY.md", 9 }, 10 { 11 url: "https://github.com/GitbookIO/javascript/blob/master/SUMMARY.md", 12 removeHeadings: true 13 }, 14 ], 15 "https://raw.githubusercontent.com/" 16 ); 17})();
For the legacy gitbook with a SUMMARY.md file at https://github.com/Open-EdTech/mostly-adequate-guide/blob/master/SUMMARY.md we see a root node like this:
1[ 2 { 3 "type": "directory", 4 "children": [ 5 { 6 "type": "file", 7 "route": "Open-EdTech/mostly-adequate-guide/ch01.md", 8 "title": "Chapter 01: What Ever Are We Doing?", 9 "rawUrl": "https://raw.githubusercontent.com/Open-EdTech/mostly-adequate-guide/master/ch01.md", 10 "ghUrl": "https://github.com/Open-EdTech/mostly-adequate-guide/blob/master/ch01.md", 11 "children": [ 12 { 13 "title": "Introductions", 14 "route": "Open-EdTech/mostly-adequate-guide/ch01.md/#introductions", 15 "type": "heading" 16 }, 17 { 18 "title": "A Brief Encounter", 19 "route": "Open-EdTech/mostly-adequate-guide/ch01.md/#a-brief-encounter", 20 "type": "heading" 21 } 22 ] 23 }, 24// ...
rawUrl
is the location where we fetch the raw text of the page from. ghUrl
is included to allow for an 'edit' this page button on each page. title
is for displaying link text in a navigation menu.
We also loaded a non-legacy GitBook from https://github.com/GitbookIO/javascript/blob/master/SUMMARY.md, non-legacy GitBooks navigate to related sub-files, not headers.
1// ... 2 { 3 "type": "directory", 4 "children": [ 5 { 6 "type": "file", 7 "route": "GitbookIO/javascript/basics/README.md", 8 "title": "Basics", 9 "children": [ 10 { 11 "type": "file", 12 "route": "GitbookIO/javascript/basics/comments.md", 13 "title": "Comments", 14 "rawUrl": "https://raw.githubusercontent.com/GitbookIO/javascript/master/basics/comments.md", 15 "ghUrl": "https://github.com/GitbookIO/javascript/blob/master/basics/comments.md" 16 }, 17 { 18 "type": "file", 19 "route": "GitbookIO/javascript/basics/variables.md", 20 "title": "Variables", 21 "rawUrl": "https://raw.githubusercontent.com/GitbookIO/javascript/master/basics/variables.md", 22 "ghUrl": "https://github.com/GitbookIO/javascript/blob/master/basics/variables.md" 23 }, 24 // ...
Here is the type of summariesToTrees.
declare type LocalConfig = {
local: true;
localPath: string;
url?: string;
removeHeadings?: boolean;
};
declare type RemoteConfig = {
localPath?: string;
url: string;
removeHeadings?: boolean;
};
export declare type Config = LocalConfig | RemoteConfig;
export declare type AllConfigs = Config[];
export default function summariesToTrees(configs: AllConfigs, rawProvider?: string): Promise<void>;
The second positional argument if not provided defaults to loading markdown from https://gitcdn.xyz/, you can override it to load from https://raw.githubusercontent.com/ at the risk of rate limiting (I have had no problems with this so far).
We support loading files locally so you don't have to push commits to GitHub to view your book. Enable local development by passing local: true
along with the path to the location of your summary file.
This script
1var {summariesToTrees} = require("next-mdx-books");
2
3(async () => {
4 await summariesToTrees(
5 [
6 {
7 local: true,
8 localPath: "C:\\Users\\matth\\OneDrive\\Documents\\GitHub\\mostly-adequate\\SUMMARY.md",
9 url:
10 "https://github.com/Open-EdTech/mostly-adequate-guide/blob/master/SUMMARY.md",
11 },
12 ],
13 );
14})();
produces this:
1[ 2 { 3 "type": "directory", 4 "children": [ 5 { 6 "type": "file", 7 "route": "Open-EdTech/mostly-adequate-guide/ch01.md", 8 "title": "Chapter 01: What Ever Are We Doing?", 9 "path": "C:\\Users\\matth\\OneDrive\\Documents\\GitHub\\mostly-adequate\\ch01.md", 10 "rawUrl": "https://gitcdn.xyz/repo/Open-EdTech/mostly-adequate-guide/master/ch01.md", 11 "ghUrl": "https://github.com/Open-EdTech/mostly-adequate-guide/blob/master/ch01.md", 12 "children": [ 13 { 14 "title": "Introductions", 15 "route": "Open-EdTech/mostly-adequate-guide/ch01.md/#introductions", 16 "type": "heading" 17 }, 18 //...
After creating bookConfig.json
we produce routes for each node in the URL tree that has a rawUrl
property. To do this we use dynamic routes in Next.js with dynamic routes, definitely be familiar with this if you want to know how this software works.
type:
1function getAllRoutesInfo(urlTrees: UrlNode[]): Promise<Record<string, FlatNode>>;
getAllRoutes
takes our URL trees and flattens them to an object that looks like this:
1{ 2 "Open-EdTech/mostly-adequate-guide/ch01.md": { 3 "route": "Open-EdTech/mostly-adequate-guide/ch01.md", 4 "title": "Chapter 01: What Ever Are We Doing?", 5 "path": "C:\\Users\\matth\\OneDrive\\Documents\\GitHub\\mostly-adequate\\ch01.md", 6 "rawUrl": "https://gitcdn.xyz/repo/Open-EdTech/mostly-adequate-guide/master/ch01.md", 7 "ghUrl": "https://github.com/Open-EdTech/mostly-adequate-guide/blob/master/ch01.md", 8 "index": 0 9 }, 10 "Open-EdTech/mostly-adequate-guide/ch02.md": { 11 "route": "Open-EdTech/mostly-adequate-guide/ch02.md", 12 "title": "Chapter 02: First Class Functions", 13 "path": "C:\\Users\\matth\\OneDrive\\Documents\\GitHub\\mostly-adequate\\ch02.md", 14 "rawUrl": "https://gitcdn.xyz/repo/Open-EdTech/mostly-adequate-guide/master/ch02.md", 15 "ghUrl": "https://github.com/Open-EdTech/mostly-adequate-guide/blob/master/ch02.md", 16 "index": 0 17 }, 18 //... 19}
We can use this allRoutesInfo
object's keys to specify the dynamic routes to create in getStaticPaths for a file named [...id].tsx.
1import { GetStaticPaths, GetStaticProps } from "next"; 2import { getAllRoutesInfo } from "next-mdx-books"; 3import bookConfig from '../bookConfig.json' 4 5export const getStaticPaths = async () => { 6 const allRoutesInfo = await getAllRoutesInfo(bookConfig); 7 return { 8 paths: Object.keys(allRoutesInfo).map((routeString) => ({ 9 params: { 10 id: routeString.split("/"), 11 }, 12 })), 13 fallback: false, 14 }; 15};
In getStaticProps we can grab some information to pass down to our page. Here, we grab the urlTree
so that each page can display a table of contents in the side navigation. We get the GitHub url, ghUrl
, to add an 'edit this page' feature.
1import { getMdSource, getAllRoutesInfo } from "next-mdx-books"; 2import renderToString from "next-mdx-remote/render-to-string"; 3import hydrate, { Source } from "next-mdx-remote/hydrate"; 4import bookConfig from '../bookConfig.json' 5 6export const getStaticProps: GetStaticProps = async ({ params }) => { 7 const allRoutesInfo = await getAllRoutesInfo(bookConfig); 8 const stringRoute = (params!.id as string[]).join("/"); 9 const nodeIndex = allRoutesInfo[stringRoute].index; 10 const ghUrl = allRoutesInfo[stringRoute].ghUrl; 11 const urlTree = bookConfig[nodeIndex]; 12 const source = await getMdSource(stringRoute, allRoutesInfo, remote); 13 const mdxSource = await renderToString(source); 14 return { 15 props: { urlTree, mdxSource, stringRoute, ghUrl }, // will be passed to the page component as props 16 }; 17}; 18//From openedtech.dev 19function Post({ urlTree, mdxSource, ghUrl }: { urlTree: UrlNode; mdxSource: Source, ghUrl: string }) { 20 const content = hydrate(mdxSource); 21 return ( 22 <SideBarProvider config={urlTree.children as StatefulNode[]}> 23 <SideBar ghUrl={ghUrl}> 24 <div className="prose dark:prose-dark max-w-sm sm:max-w-md lg:max-w-xl xl:max-w-2xl m-auto px-2 flex-1 "> 25 <div>{content}</div> 26 </div> 27 </SideBar> 28 </SideBarProvider> 29 ); 30}
No vulnerabilities found.
No security vulnerabilities found.