Skip to content

Commit

Permalink
feat(endpoint-micropub): use md5 hash of published date as final opti…
Browse files Browse the repository at this point in the history
…on for a slug

this means the value stays the same, and doesn’t change each time a post is re-created
  • Loading branch information
paulrobertlloyd committed Oct 8, 2024
1 parent b09a8c4 commit 2ea5a8c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 10 deletions.
4 changes: 0 additions & 4 deletions helpers/fixtures/jf2/note-slug-missing-no-name.jf2

This file was deleted.

8 changes: 3 additions & 5 deletions packages/endpoint-micropub/lib/jf2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDate, randomString, slugify } from "@indiekit/util";
import { getDate, md5, slugify } from "@indiekit/util";
import {
fetchReferences,
mf2tojf2,
Expand Down Expand Up @@ -236,17 +236,15 @@ export const getVideoProperty = (properties, me) => {
*/
export const getSlugProperty = (properties, separator) => {
const suggested = properties["mp-slug"];
const { name } = properties;
const { name, published } = properties;

let string;
if (suggested) {
string = suggested;
} else if (name) {
string = excerptString(name, 5);
} else {
string = randomString(5)
.replace("_", "0") // Slugify function strips any leading underscore
.replace(separator, "0"); // Don’t include slug separator character
string = md5(published).slice(0, 5);
}

return slugify(string, { separator });
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint-micropub/test/unit/jf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ describe("endpoint-micropub/lib/jf2", () => {

it("Derives slug by generating random string", () => {
const properties = JSON.parse(
getFixture("jf2/note-slug-missing-no-name.jf2"),
getFixture("jf2/note-published-provided.jf2"),
);
const result = getSlugProperty(properties, "-");

Expand Down

0 comments on commit 2ea5a8c

Please sign in to comment.