mirror of
https://github.com/by-jp/www.byjp.me.git
synced 2025-08-10 19:05:41 +01:00
[Indiekit] Adapt inbound hashtags
This commit is contained in:
parent
6851285e75
commit
aac4d1184b
2 changed files with 45 additions and 1 deletions
|
@ -52,6 +52,7 @@ const getFrontMatter = (properties, frontMatterFormat) => {
|
||||||
}),
|
}),
|
||||||
...properties,
|
...properties,
|
||||||
};
|
};
|
||||||
|
// TODO: move photos
|
||||||
|
|
||||||
delete properties.content; // Shown below front matter
|
delete properties.content; // Shown below front matter
|
||||||
delete properties.deleted; // Use `expiryDate`
|
delete properties.deleted; // Use `expiryDate`
|
||||||
|
@ -93,8 +94,29 @@ const getFrontMatter = (properties, frontMatterFormat) => {
|
||||||
* @returns {string} Rendered template
|
* @returns {string} Rendered template
|
||||||
*/
|
*/
|
||||||
export const getPostTemplate = (properties, frontMatterFormat = "yaml") => {
|
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);
|
const frontMatter = getFrontMatter(properties, frontMatterFormat);
|
||||||
|
|
||||||
return frontMatter + content;
|
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<string>}} 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 }
|
||||||
|
}
|
|
@ -210,6 +210,28 @@ syndication: https://website.example/post/12345
|
||||||
---
|
---
|
||||||
|
|
||||||
I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.
|
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.
|
||||||
`,
|
`,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue