diff --git a/indiekit/packages/indiekit-preset-byjp/lib/post-template.js b/indiekit/packages/indiekit-preset-byjp/lib/post-template.js index b17f6a14..1884c249 100644 --- a/indiekit/packages/indiekit-preset-byjp/lib/post-template.js +++ b/indiekit/packages/indiekit-preset-byjp/lib/post-template.js @@ -52,6 +52,7 @@ const getFrontMatter = (properties, frontMatterFormat) => { }), ...properties, }; + // TODO: move photos delete properties.content; // Shown below front matter delete properties.deleted; // Use `expiryDate` @@ -93,8 +94,29 @@ const getFrontMatter = (properties, frontMatterFormat) => { * @returns {string} Rendered template */ export const getPostTemplate = (properties, frontMatterFormat = "yaml") => { - const content = getContent(properties); + const { content, tags } = replaceTags(getContent(properties)); + if (tags.length > 0) { + properties.tags = tags; + } const frontMatter = getFrontMatter(properties, frontMatterFormat); return frontMatter + content; }; + +const tagFinder = /(?<=\s|^)#[a-z0-9]+\b/ig; + +/** + * Replaces all #HashTags with [HashTags](/tags/hashtags) and returns a compact array of the tags used + * @param {string} content - Post content + * @returns {{content: string, tags: Array}} The replaced content, and found tags + */ +const replaceTags = (content) => { + let tags = []; + content = content.replace(tagFinder, (tag) => { + tag = tag.substring(1); + tags.push(tag); + return `[${tag}](/tags/${tag.toLowerCase()})`; + }); + + return { content, tags } +} \ No newline at end of file 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 9638090e..6690a0ab 100644 --- a/indiekit/packages/indiekit-preset-byjp/test/unit/post-template.js +++ b/indiekit/packages/indiekit-preset-byjp/test/unit/post-template.js @@ -210,6 +210,28 @@ syndication: https://website.example/post/12345 --- I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice. +`, + ); + }); + + it("Renders posts with hashtags correctly", () => { + const result = getPostTemplate({ + published: "2024-10-02", + content: "#some posts might have #hashtags, even #MultiCasedOnes but [fragment URLs](#heading) must be left alone.", + }); + + assert.equal( + result, + `--- +date: 2024-10-02 +publishDate: 2024-10-02 +tags: + - some + - hashtags + - MultiCasedOnes +--- + +[some](/tags/some) posts might have [hashtags](/tags/hashtags), even [MultiCasedOnes](/tags/multicasedones) but [fragment URLs](#heading) must be left alone. `, ); });