Skip to content

Commit 0581e92

Browse files
committed
fix: only show ellipsis for truncated blog teasers
1 parent 40a9a4b commit 0581e92

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

src/utilities/content-tree-enhancers.mjs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,16 @@ export const enhance = (tree, options) => {
7373

7474
const isBlogItem = normalizedPath.includes("/blog/");
7575
if (isBlogItem) {
76-
const teaser = (body || "")
76+
const teaserLines = (body || "")
7777
.split("\n")
78-
.filter((line) => line.trim() && !line.trim().startsWith("#"))
78+
.filter((line) => line.trim() && !line.trim().startsWith("#"));
79+
const teaserText = teaserLines
7980
.slice(0, 3)
8081
.join(" ")
81-
.replaceAll(/\[([^\]]+)\]\([^)]+\)/g, "$1") // Strip markdown links but keep text
82-
.slice(0, 240);
83-
tree.teaser = `${teaser}...`;
82+
.replaceAll(/\[([^\]]+)\]\([^)]+\)/g, "$1"); // Strip markdown links but keep text
83+
const teaser = teaserText.slice(0, 240);
84+
const isTruncated = teaserLines.length > 3 || teaserText.length > 240;
85+
tree.teaser = isTruncated ? `${teaser}...` : teaser;
8486
}
8587

8688
Object.assign(

src/utilities/content-tree-enhancers.test.mjs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import fs from "node:fs";
2+
import os from "node:os";
3+
import path from "node:path";
14
// eslint-disable-next-line import/no-extraneous-dependencies
25
import { describe, expect } from "@jest/globals";
3-
import { restructure } from "./content-tree-enhancers.mjs";
6+
import { enhance, restructure } from "./content-tree-enhancers.mjs";
47

58
describe("restructure", () => {
69
it("applies filter result back to children array", () => {
@@ -55,3 +58,41 @@ describe("restructure", () => {
5558
expect(root.children.map((item) => item.title)).toEqual(["API", "Guides"]);
5659
});
5760
});
61+
62+
describe("enhance", () => {
63+
const createBlogTree = (body) => {
64+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "webpack-blog-"));
65+
const blogDir = path.join(root, "blog");
66+
fs.mkdirSync(blogDir);
67+
const filePath = path.join(blogDir, "example.mdx");
68+
fs.writeFileSync(filePath, `---\ntitle: Example\n---\n\n${body}`);
69+
70+
return {
71+
root,
72+
tree: {
73+
type: "file",
74+
path: filePath,
75+
extension: ".mdx",
76+
name: "example.mdx",
77+
},
78+
};
79+
};
80+
81+
it("does not append an ellipsis to an untruncated blog teaser", () => {
82+
const { root, tree } = createBlogTree("Short body.");
83+
84+
enhance(tree, { dir: root });
85+
86+
expect(tree.teaser).toBe("Short body.");
87+
});
88+
89+
it("appends an ellipsis when the blog teaser is truncated", () => {
90+
const { root, tree } = createBlogTree(
91+
["First line.", "Second line.", "Third line.", "Fourth line."].join("\n"),
92+
);
93+
94+
enhance(tree, { dir: root });
95+
96+
expect(tree.teaser).toBe("First line. Second line. Third line....");
97+
});
98+
});

0 commit comments

Comments
 (0)