Skip to content

Commit

Permalink
Add breadcrumb navigation support and update related configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewshawcare committed Feb 19, 2025
1 parent aff03e4 commit 0955426
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 4 deletions.
2 changes: 2 additions & 0 deletions markdoc-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import HeadingNode from "./nodes/heading/schema.js";
import DiagramTag from "./tags/diagram/schema.js";
import TableOfContentsTag from "./tags/table-of-contents/schema.js";
import HtmlTagTag from "./tags/html-tag/schema.js";
import BreadCrumbNavigationTag from "./tags/breadcrumb-navigation/schema.js";

const config: Config = {
nodes: {
Expand All @@ -12,6 +13,7 @@ const config: Config = {
diagram: DiagramTag,
"table-of-contents": TableOfContentsTag,
"html-tag": HtmlTagTag,
"breadcrumb-navigation": BreadCrumbNavigationTag,
},
};

Expand Down
4 changes: 4 additions & 0 deletions schemas/variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"destinationDirectory": {
"type": "string"
},
"contentDirectory": {
"type": "string"
},
"filename": {
"type": "string"
},
Expand All @@ -21,6 +24,7 @@
"required": [
"frontmatter",
"sourceDirectory",
"contentDirectory",
"destinationDirectory",
"filename"
],
Expand Down
2 changes: 2 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { FilterProcessor } from "../middleware/filter-processor.js";
import { SequentialProcessor } from "../middleware/sequential-processor.js";

const destinationDirectory = Path.resolve("dist");
const contentDirectory = Path.resolve("content");

await compileStaticAssets({
destinationDirectory,
Expand All @@ -26,6 +27,7 @@ await compileStaticAssets({

await compileMarkdocDocuments({
destinationDirectory,
contentDirectory,
sourceGlobs: [
{
pattern: "**/*.md",
Expand Down
3 changes: 3 additions & 0 deletions scripts/compile-markdoc-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ type Glob = {

export const compileMarkdocDocuments = async ({
destinationDirectory,
contentDirectory,
sourceGlobs,
layoutGlobs,
partialGlobs,
defaultLayout,
renderMiddleware,
}: {
destinationDirectory: string;
contentDirectory: string;
sourceGlobs: Glob[];
layoutGlobs: Glob[];
partialGlobs: Glob[];
Expand Down Expand Up @@ -188,6 +190,7 @@ export const compileMarkdocDocuments = async ({
frontmatter,
sourceDirectory: sourcePath.dir,
destinationDirectory: destinationPath.dir,
contentDirectory,
filename: sourcePath.base,
};

Expand Down
10 changes: 6 additions & 4 deletions static/index.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
body {
margin: 3rem auto;
max-width: 60ch;
font-family: "Inter", sans-serif;
line-height: 2rem;
margin: 6rem 0;
}

article {
margin: 1rem auto;
max-width: 60ch;
.breadcrumb.navigation {
.arrow {
padding: 0 0.5rem;
}
}
93 changes: 93 additions & 0 deletions tags/breadcrumb-navigation/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import FileSystem from "node:fs";
import Path from "node:path";
import JSYAML from "js-yaml";
import Markdoc, { Schema } from "@markdoc/markdoc";
import { validateTypeUsingSchema } from "../../validate-type-using-schema.js";
import {
default as variablesSchema,
Schema as Variables,
} from "../../schemas/variables.json.js";
import {
default as frontmatterSchema,
Schema as Frontmatter,
} from "../../schemas/frontmatter.json.js";

const getVariablesFromConfig = (config: Markdoc.Config) =>
validateTypeUsingSchema<Variables>(
config.variables,
variablesSchema,
);

const addBreadcrumbs = async (navTag: Markdoc.Tag, config: Markdoc.Config) => {
const { sourceDirectory, contentDirectory, filename } = getVariablesFromConfig(config);

const pathname = Path.join(sourceDirectory.replace(contentDirectory, ""), filename);
const pathparts = pathname.split("/");

let breadcrumbPathParts = [];

console.log({contentDirectory, pathname, pathparts})

for (const pathpart of pathparts) {
breadcrumbPathParts.push(pathpart);
const breadcrumbRelativePath = Path.join(...breadcrumbPathParts);

console.log({breadcrumbPathParts, breadcrumbRelativePath})

let path = Path.resolve(contentDirectory);

// If the paths are the same and end in an index, we are at a directory, otherwise we need to continue
if (breadcrumbPathParts.join("/") === pathname) {
if (breadcrumbRelativePath.endsWith("index.md")) {
break;
}
path = Path.resolve(path, breadcrumbRelativePath);
} else {
path = Path.resolve(path, breadcrumbRelativePath, "index.md");
}

console.log({path})

const articleContents = await FileSystem.promises.readFile(
path,
"utf8"
);

const articleNode = Markdoc.parse(articleContents);

const frontmatter = validateTypeUsingSchema<Frontmatter>(
JSYAML.load(articleNode.attributes.frontmatter),
frontmatterSchema,
);

const anchorTag = new Markdoc.Tag("a");

anchorTag.children.push(frontmatter.title || "");

anchorTag.attributes.href = `${breadcrumbPathParts.join("/")}/`;

navTag.children.push(anchorTag);

navTag.children.push(new Markdoc.Tag("span", { class: "arrow" }, ["→"]));
}

navTag.children.pop();

if (navTag.children.length > 0) {
const lastChild = navTag.children[navTag.children.length - 1];
if (lastChild instanceof Markdoc.Tag) {
delete lastChild.attributes.href;
}
}
}

const schema: Schema = {
render: "nav",
async transform(node, config) {
const navTag = new Markdoc.Tag("nav", { class: "breadcrumb navigation" });
await addBreadcrumbs(navTag, config);
return navTag;
}
};

export default schema;
2 changes: 2 additions & 0 deletions templates/layouts/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<body>

{% breadcrumb-navigation /%}

<article class="article">

<header>
Expand Down
2 changes: 2 additions & 0 deletions templates/layouts/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<body>

{% breadcrumb-navigation /%}

<article>

# {% $frontmatter.title %}
Expand Down
2 changes: 2 additions & 0 deletions templates/layouts/note.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<body>

{% breadcrumb-navigation /%}

<article class="note">

<header>
Expand Down

0 comments on commit 0955426

Please sign in to comment.