Compare commits

...

4 Commits

Author SHA1 Message Date
Aaron Pham
fac3ef796e
chore: only set this if not showCurrentPage is set. 2024-08-03 04:25:46 -04:00
Aaron Pham
c73e64e1d2
chore(breadcrumbs): Add slug for breadcrumbs on tag page
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
2024-08-03 03:48:49 -04:00
Aaron Pham
50c78cdf63
fix(logic): increment check with unique maps
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
2024-08-03 03:18:58 -04:00
Aaron Pham
0ab1a2ef45
feat(breadcrumb): add optional path-style
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
2024-08-03 02:16:06 -04:00
2 changed files with 45 additions and 0 deletions

View File

@ -21,6 +21,7 @@ Component.Breadcrumbs({
resolveFrontmatterTitle: true, // whether to resolve folder names through frontmatter titles resolveFrontmatterTitle: true, // whether to resolve folder names through frontmatter titles
hideOnRoot: true, // whether to hide breadcrumbs on root `index.md` page hideOnRoot: true, // whether to hide breadcrumbs on root `index.md` page
showCurrentPage: true, // whether to display the current page in the breadcrumbs showCurrentPage: true, // whether to display the current page in the breadcrumbs
style: "full", // determine the style of breadcrumb, currently support `full`, `letter`, `unique`.
}) })
``` ```

View File

@ -9,6 +9,8 @@ type CrumbData = {
path: string path: string
} }
type CrumbStyle = "full" | "letter" | "unique"
interface BreadcrumbOptions { interface BreadcrumbOptions {
/** /**
* Symbol between crumbs * Symbol between crumbs
@ -30,6 +32,17 @@ interface BreadcrumbOptions {
* Whether to display the current page in the breadcrumbs. * Whether to display the current page in the breadcrumbs.
*/ */
showCurrentPage: boolean showCurrentPage: boolean
/**
* Set a style for breadcrumbs. The following are supported:
* - full (default): show the full path of the breadcrumb.
* - letter: works like full, but will write every folder name using first letter only. The last folder will be displayed in full. For example:
* - `folder` will be shorten to `f`
* - `.config` will be shorten to `.c`
* - unique: works like `letter`, but will make sure every folder name is shortest unique value. For example:
* - `path/path/path/to/file.md` with `unique` will set `p/pa/pat/path/to/file.md`.
* - However, uniqueness does not refer different folder at the same level. For example: `path1/file.md` and `path2/file.md` will both show `p/file.md`
*/
style: CrumbStyle
} }
const defaultOptions: BreadcrumbOptions = { const defaultOptions: BreadcrumbOptions = {
@ -38,6 +51,7 @@ const defaultOptions: BreadcrumbOptions = {
resolveFrontmatterTitle: true, resolveFrontmatterTitle: true,
hideOnRoot: true, hideOnRoot: true,
showCurrentPage: true, showCurrentPage: true,
style: "full",
} }
function formatCrumb(displayName: string, baseSlug: FullSlug, currentSlug: SimpleSlug): CrumbData { function formatCrumb(displayName: string, baseSlug: FullSlug, currentSlug: SimpleSlug): CrumbData {
@ -88,6 +102,9 @@ export default ((opts?: Partial<BreadcrumbOptions>) => {
// full path until current part // full path until current part
let currentPath = "" let currentPath = ""
// Map to store the shortened names for each path segment
const shortenedNames: Map<string, string> = new Map()
for (let i = 0; i < slugParts.length - 1; i++) { for (let i = 0; i < slugParts.length - 1; i++) {
let curPathSegment = slugParts[i] let curPathSegment = slugParts[i]
@ -104,6 +121,29 @@ export default ((opts?: Partial<BreadcrumbOptions>) => {
currentPath = joinSegments(currentPath, slugParts[i]) currentPath = joinSegments(currentPath, slugParts[i])
const includeTrailingSlash = !isTagPath || i < 1 const includeTrailingSlash = !isTagPath || i < 1
switch (options.style) {
case "letter":
if (curPathSegment.startsWith(".")) {
curPathSegment = curPathSegment.slice(0, 2)
} else {
curPathSegment = curPathSegment.charAt(0)
}
break
case "unique":
let shortenedName = curPathSegment.charAt(0)
let uniqueName = shortenedName
let counter = 1
while (shortenedNames.has(uniqueName)) {
uniqueName = curPathSegment.slice(0, counter + 1)
counter++
}
shortenedNames.set(uniqueName, currentPath)
curPathSegment = uniqueName
break
}
// Format and add current crumb // Format and add current crumb
const crumb = formatCrumb( const crumb = formatCrumb(
curPathSegment, curPathSegment,
@ -113,6 +153,10 @@ export default ((opts?: Partial<BreadcrumbOptions>) => {
crumbs.push(crumb) crumbs.push(crumb)
} }
if (isTagPath && !options.showCurrentPage) {
crumbs.push({ displayName: slugParts.at(-1) ?? "", path: "" })
}
// Add current file to crumb (can directly use frontmatter title) // Add current file to crumb (can directly use frontmatter title)
if (options.showCurrentPage && slugParts.at(-1) !== "index") { if (options.showCurrentPage && slugParts.at(-1) !== "index") {
crumbs.push({ crumbs.push({