Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/utilities/content-tree-enhancers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ export const enhance = (tree, options) => {

const isBlogItem = normalizedPath.includes("/blog/");
if (isBlogItem) {
const teaser = (body || "")
const teaserLines = (body || "")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no point changing variable names and its prototype chaining

.split("\n")
.filter((line) => line.trim() && !line.trim().startsWith("#"))
.filter((line) => line.trim() && !line.trim().startsWith("#"));
const teaserText = teaserLines
.slice(0, 3)
.join(" ")
.replaceAll(/\[([^\]]+)\]\([^)]+\)/g, "$1") // Strip markdown links but keep text
.slice(0, 240);
tree.teaser = `${teaser}...`;
.replaceAll(/\[([^\]]+)\]\([^)]+\)/g, "$1"); // Strip markdown links but keep text
const teaser = teaserText.slice(0, 240);
const isTruncated = teaserLines.length > 3 || teaserText.length > 240;
tree.teaser = isTruncated ? `${teaser}...` : teaser;
}

Object.assign(
Expand Down
43 changes: 42 additions & 1 deletion src/utilities/content-tree-enhancers.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
// eslint-disable-next-line import/no-extraneous-dependencies
import { describe, expect } from "@jest/globals";
import { restructure } from "./content-tree-enhancers.mjs";
import { enhance, restructure } from "./content-tree-enhancers.mjs";

describe("restructure", () => {
it("applies filter result back to children array", () => {
Expand Down Expand Up @@ -55,3 +58,41 @@ describe("restructure", () => {
expect(root.children.map((item) => item.title)).toEqual(["API", "Guides"]);
});
});

describe("enhance", () => {
const createBlogTree = (body) => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "webpack-blog-"));
const blogDir = path.join(root, "blog");
fs.mkdirSync(blogDir);
const filePath = path.join(blogDir, "example.mdx");
fs.writeFileSync(filePath, `---\ntitle: Example\n---\n\n${body}`);

return {
root,
tree: {
type: "file",
path: filePath,
extension: ".mdx",
name: "example.mdx",
},
};
};

it("does not append an ellipsis to an untruncated blog teaser", () => {
const { root, tree } = createBlogTree("Short body.");

enhance(tree, { dir: root });

expect(tree.teaser).toBe("Short body.");
});

it("appends an ellipsis when the blog teaser is truncated", () => {
const { root, tree } = createBlogTree(
["First line.", "Second line.", "Third line.", "Fourth line."].join("\n"),
);

enhance(tree, { dir: root });

expect(tree.teaser).toBe("First line. Second line. Third line....");
});
});
Loading