From e964c418992fc1366a15c8f0b6a08ceb7e5ebf86 Mon Sep 17 00:00:00 2001 From: JP Hastings-Spital Date: Tue, 8 Oct 2024 12:27:53 +0100 Subject: [PATCH] [IndieKit] Add peopletag parsing Allows me to reference @People in Indiekit-powered posts, so I don't need to write {{< friend "people" >}} in longhand. --- .../indiekit-preset-byjp/lib/post-template.js | 7 +++++-- .../test/unit/post-template.js | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/indiekit/packages/indiekit-preset-byjp/lib/post-template.js b/indiekit/packages/indiekit-preset-byjp/lib/post-template.js index 1884c249..1a5dcc22 100644 --- a/indiekit/packages/indiekit-preset-byjp/lib/post-template.js +++ b/indiekit/packages/indiekit-preset-byjp/lib/post-template.js @@ -103,7 +103,8 @@ export const getPostTemplate = (properties, frontMatterFormat = "yaml") => { return frontMatter + content; }; -const tagFinder = /(?<=\s|^)#[a-z0-9]+\b/ig; +const hashtagFinder = /(?<=\s|^)#[a-z0-9]+\b/ig; +const peopletagFinder = /(?<=\s|^)@[a-z]+\b/ig; /** * Replaces all #HashTags with [HashTags](/tags/hashtags) and returns a compact array of the tags used @@ -112,10 +113,12 @@ const tagFinder = /(?<=\s|^)#[a-z0-9]+\b/ig; */ const replaceTags = (content) => { let tags = []; - content = content.replace(tagFinder, (tag) => { + content = content.replace(hashtagFinder, (tag) => { tag = tag.substring(1); tags.push(tag); return `[${tag}](/tags/${tag.toLowerCase()})`; + }).replace(peopletagFinder, (tag) => { + return `{{< friend "${tag.substring(1).toLowerCase()}" >}}`; }); return { content, tags } diff --git a/indiekit/packages/indiekit-preset-byjp/test/unit/post-template.js b/indiekit/packages/indiekit-preset-byjp/test/unit/post-template.js index 6690a0ab..40074e3d 100644 --- a/indiekit/packages/indiekit-preset-byjp/test/unit/post-template.js +++ b/indiekit/packages/indiekit-preset-byjp/test/unit/post-template.js @@ -232,6 +232,24 @@ tags: --- [some](/tags/some) posts might have [hashtags](/tags/hashtags), even [MultiCasedOnes](/tags/multicasedones) but [fragment URLs](#heading) must be left alone. +`, + ); + }); + + it("Renders posts with people tags correctly", () => { + const result = getPostTemplate({ + published: "2024-10-02", + content: "Sometimes I reference @People, but [@fedi@example.com](https://example.com/@fedi) links and email@example.com shouldn't be changed.", + }); + + assert.equal( + result, + `--- +date: 2024-10-02 +publishDate: 2024-10-02 +--- + +Sometimes I reference {{< friend "people" >}}, but [@fedi@example.com](https://example.com/@fedi) links and email@example.com shouldn't be changed. `, ); });