Skip to content

Commit

Permalink
fix BEGIN_MAGIC regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Aug 22, 2023
1 parent 0ba9b46 commit f5930af
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/lib/getExcerpt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ describe("getExcerpt", () => {

expect(result).toBe("foo bar...");
});

it("does not include private notes", () => {
const result = getExcerpt(`private notes
BEGIN_MAGIC
content`);

expect(result).not.toContain("private notes");
});
});
7 changes: 3 additions & 4 deletions src/lib/getExcerpt.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import getDom from "./getDom";
import trimContent from "./trimContent";

const EXCERPT_LENGTH = 300;

export default function getExcerpt(html: string): string {
const { document } = getDom(html);

document.body.innerHTML = html;

const trimmed = trimContent(html);
const { document } = getDom(trimmed);
const footnotes = Array.from(document.querySelectorAll("a.footnote"));

footnotes.forEach((el) => el.remove());
Expand Down
63 changes: 63 additions & 0 deletions src/schemas/post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,67 @@ body`;

expect(p.disqus_id).toEqual("test-post");
});

it("does not include private notes in excerpts", async () => {
const md = `---
title: "Test Post"
slug: "test-post"
date: 2021-01-01
author: "dreeves"
disqus_id: "test-post"
---
private notes
BEGIN_MAGIC
content`;

const p = post.parse({
url: "the_url",
md,
});

expect(p.excerpt).not.toContain("private notes");
});

it("does not include raw markdown in excerpts", async () => {
const md = `---
title: "Test Post"
slug: "test-post"
date: 2021-01-01
author: "dreeves"
disqus_id: "test-post"
---
[link](#)`;

const p = post.parse({
url: "the_url",
md,
});

expect(p.excerpt).not.toContain("(#)");
});

it("does not use image from private notes", async () => {
const md = `---
title: "Test Post"
slug: "test-post"
date: 2021-01-01
author: "dreeves"
disqus_id: "test-post"
---
<img src='/private' />
BEGIN_MAGIC
content`;

const p = post.parse({
url: "the_url",
md,
});

expect(p.image).toBeUndefined();
});
});
9 changes: 5 additions & 4 deletions src/schemas/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ export const post = z
...legacyPost.parse(url),
...frontmatter.parse(data),
};
const c = body.parse(content);

return {
...meta,
excerpt: meta.excerpt || getExcerpt(content),
image: extractImage(content),
excerpt: meta.excerpt || getExcerpt(c),
image: extractImage(c),
title: meta.title || parseTitle(md),
date_string: dateString.parse(meta.date),
content,
content: c,
};
})
.pipe(
z.object({
content: body,
content: z.string(),
excerpt: z.string(),
slug: z.string(),
image: image.optional(),
Expand Down

0 comments on commit f5930af

Please sign in to comment.