Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | 10x 10x 10x 19x 19x 624x 21x 21x 603x 1x 1x 602x 577x 577x 577x 577x 577x 7x 7x 577x 577x 624x 50x 532x 4x 528x 528x 191x 4x 187x 187x 19x 19x 19x 19x 19x 19x 19x 19x 191x 191x 191x 191x 191x 191x 19x 577x 38x 38x 38x 38x 539x 539x 149x 149x 149x 7x 7x 142x 135x 135x 7x 7x 390x 49x 49x 49x 341x 341x 341x 19x | /**
* Shared navigation.md parser.
*
* Written in plain `.mjs` so it can be consumed by:
* - `src/lib/parse-navigation-md.ts` (bundled with the Next app)
* - `scripts/verify-navigation.mjs` (plain-node prebuild step)
*
* Types are declared inline via JSDoc `@typedef` so TS consumers get the
* same shapes without a separate `.d.mts` file.
*
* Format:
* - Category Name (indent < 2 => category header)
* - [Page Name](/cat/slug) (indent < 4 => page)
* - [Tab Name](/cat/slug--tab) (indent >= 4 => sub-tab of parent page)
*
* Groups nest pages one level deeper, mirroring Zeroheight's sidebar:
* - [Group Page](/cat/group) {group} (page group: a page that lists member pages)
* - Group Label (label group: a plain-text heading, no page)
* - [Member](/cat/member) (indent 4 inside a group => member page)
* - [Tab](/cat/member--tab) (indent >= 6 inside a group => member tab)
*
* A group ends at the next indent-2 line. Tab characters are expanded to
* 2 spaces before depth is measured. Flags are `{name}` tokens after the link;
* `KNOWN_FLAGS` lists the ones the parser understands.
*/
/**
* @typedef {Object} NavTab
* @property {string} name
* @property {string} slug
* @property {string} url
*
* @typedef {Object} NavPageGroup
* @property {string} name
* @property {string} [url] Set for page groups, unset for label groups.
*
* @typedef {Object} NavPage
* @property {number} id
* @property {string} name
* @property {string} slug
* @property {string} url
* @property {string} [category]
* @property {NavTab[]} [tabs]
* @property {boolean} hidden
* @property {NavPageGroup} [group] The group this page is a member of.
*
* @typedef {Object} NavGroup
* @property {'group'} kind
* @property {number} id
* @property {string} name
* @property {NavPage} [page] The group's own page (page groups only).
* @property {NavPage[]} children
*
* @typedef {Object} NavCategory
* @property {number} id
* @property {string} name
* @property {string} slug
* @property {boolean} hidden
* @property {NavPage[]} children Every page in nav order, group pages and members included.
* @property {(NavPage | NavGroup)[]} items The sidebar tree: top-level pages and groups.
*
* @typedef {Object} NavLine
* @property {number} indent
* @property {string} content
* @property {{ name: string, url: string } | null} link
* @property {string[]} flags
*/
export const KNOWN_FLAGS = ['group'];
const LINK_RE = /^\[(.+?)\]\((.+?)\)$/;
const TRAILING_FLAG_RE = /\{([a-z-]+)\}$/;
/**
* Yield one entry per meaningful line (skips HTML comments, blanks, `#` docs).
* @param {string} raw
* @returns {Generator<NavLine>}
*/
export function* iterNavLines(raw) {
let inComment = false;
for (const line of raw.split('\n')) {
if (inComment) {
if (line.includes('-->')) inComment = false;
continue;
}
if (line.trimStart().startsWith('<!--')) {
Eif (!line.includes('-->')) inComment = true;
continue;
}
if (!line.trim() || line.startsWith('#')) continue;
const expanded = line.replace(/\t/g, ' ');
const indent = expanded.length - expanded.trimStart().length;
let content = expanded.trim().replace(/^-\s*/, '');
const flags = [];
let m;
while ((m = content.match(TRAILING_FLAG_RE))) {
flags.unshift(m[1]);
content = content.slice(0, m.index).trimEnd();
}
const linkMatch = content.match(LINK_RE);
const link = linkMatch ? { name: linkMatch[1], url: linkMatch[2] } : null;
yield { indent, content, link, flags };
}
}
function nameToSlug(name) {
return name
.toLowerCase()
.replace(/[^a-z0-9\s-]/g, '')
.trim()
.replace(/\s+/g, '-');
}
function slugFromUrl(url, fallbackName) {
if (/^https?:\/\//.test(url)) {
return (fallbackName ? nameToSlug(fallbackName) : '') || 'external';
}
const parts = url.replace(/^\//, '').split('/');
return parts[parts.length - 1] || (fallbackName ? nameToSlug(fallbackName) : '');
}
function categoryFromUrl(url, parentCategory) {
if (/^https?:\/\//.test(url)) {
return (parentCategory ? nameToSlug(parentCategory) : '') || 'external';
}
const parts = url.replace(/^\//, '').split('/');
return parts[0] || undefined;
}
/**
* Parse a raw navigation.md string into NavCategory[].
* @param {string} raw
* @returns {NavCategory[]}
*/
export function parseNavigationMdString(raw) {
const categories = [];
let catId = 0;
let pageId = 0;
let groupId = 0;
let currentCategory = null;
let currentGroup = null;
let currentPage = null;
const addPage = (link, group) => {
const pageCat = categoryFromUrl(link.url, currentCategory.name);
Iif (pageCat && currentCategory.children.length === 0 && currentCategory.slug !== pageCat) {
currentCategory.slug = pageCat;
}
const page = {
id: ++pageId,
name: link.name,
slug: slugFromUrl(link.url, link.name),
url: link.url,
category: pageCat,
hidden: false,
};
if (group) page.group = group;
currentCategory.children.push(page);
return page;
};
for (const { indent, content, link, flags } of iterNavLines(raw)) {
if (indent < 2) {
currentCategory = {
id: ++catId,
name: link ? link.name : content,
slug: (link ? categoryFromUrl(link.url) : undefined) ?? nameToSlug(link ? link.name : content),
hidden: false,
children: [],
items: [],
};
categories.push(currentCategory);
currentGroup = null;
currentPage = null;
} else Iif (!currentCategory) {
continue;
} else if (indent < 4) {
currentGroup = null;
currentPage = null;
if (link && flags.includes('group')) {
currentGroup = { kind: 'group', id: ++groupId, name: link.name, page: addPage(link), children: [] };
currentCategory.items.push(currentGroup);
} else if (link) {
currentPage = addPage(link);
currentCategory.items.push(currentPage);
} else {
currentGroup = { kind: 'group', id: ++groupId, name: content, children: [] };
currentCategory.items.push(currentGroup);
}
} else if (currentGroup && indent < 6) {
Iif (!link) continue;
currentPage = addPage(link, { name: currentGroup.name, url: currentGroup.page?.url });
currentGroup.children.push(currentPage);
} else {
Iif (!link || !currentPage) continue;
if (!currentPage.tabs) currentPage.tabs = [];
currentPage.tabs.push({
name: link.name,
slug: slugFromUrl(link.url),
url: link.url,
});
}
}
return categories;
}
|