From a3ad0faaa176946065955a7f056340d4f514b77a Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Sun, 12 Nov 2023 15:51:05 +0000 Subject: [PATCH] fix(endpoint-micropub): fetch references for form-encoded requests --- indiekit.config.js | 1 + packages/endpoint-micropub/lib/controllers/action.js | 2 +- packages/endpoint-micropub/lib/jf2.js | 10 ++++++++-- packages/endpoint-micropub/tests/unit/jf2.js | 4 ++-- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/indiekit.config.js b/indiekit.config.js index a79d14716..aa50c51d1 100644 --- a/indiekit.config.js +++ b/indiekit.config.js @@ -23,6 +23,7 @@ const config = { publication: { me: process.env.PUBLICATION_URL, categories: ["internet", "indieweb", "indiekit", "test", "testing"], + enrichPostData: true, }, "@indiekit/store-github": { user: process.env.GITHUB_USER, diff --git a/packages/endpoint-micropub/lib/controllers/action.js b/packages/endpoint-micropub/lib/controllers/action.js index b3f42a90e..7db5e5c4c 100644 --- a/packages/endpoint-micropub/lib/controllers/action.js +++ b/packages/endpoint-micropub/lib/controllers/action.js @@ -44,7 +44,7 @@ export const actionController = async (request, response, next) => { // Create and normalise JF2 data jf2 = request.is("json") ? await mf2ToJf2(body, publication.enrichPostData) - : formEncodedToJf2(body); + : await formEncodedToJf2(body, publication.enrichPostData); // Attach files jf2 = files diff --git a/packages/endpoint-micropub/lib/jf2.js b/packages/endpoint-micropub/lib/jf2.js index 23c301162..c296bd8ef 100644 --- a/packages/endpoint-micropub/lib/jf2.js +++ b/packages/endpoint-micropub/lib/jf2.js @@ -1,5 +1,6 @@ import { getDate, randomString, slugify } from "@indiekit/util"; import { mf2tojf2, mf2tojf2referenced } from "@paulrobertlloyd/mf2tojf2"; +import { fetchReferences } from "@paulrobertlloyd/mf2tojf2/lib/fetch-references.js"; import { markdownToHtml, htmlToMarkdown } from "./markdown.js"; import { reservedProperties } from "./reserved-properties.js"; import { @@ -12,9 +13,10 @@ import { /** * Create JF2 object from form-encoded request * @param {object} body - Form-encoded request body - * @returns {object} Micropub action + * @param {boolean} requestReferences - Request data for any referenced URLs + * @returns {Promise} Micropub action */ -export const formEncodedToJf2 = (body) => { +export const formEncodedToJf2 = async (body, requestReferences) => { const jf2 = { type: body.h || "entry", }; @@ -37,6 +39,10 @@ export const formEncodedToJf2 = (body) => { } } + if (requestReferences) { + return fetchReferences(jf2); + } + return jf2; }; diff --git a/packages/endpoint-micropub/tests/unit/jf2.js b/packages/endpoint-micropub/tests/unit/jf2.js index 4f509ae90..6d48516fc 100644 --- a/packages/endpoint-micropub/tests/unit/jf2.js +++ b/packages/endpoint-micropub/tests/unit/jf2.js @@ -30,8 +30,8 @@ test.beforeEach((t) => { }; }); -test("Creates JF2 object from form-encoded request", (t) => { - const result = formEncodedToJf2({ +test("Creates JF2 object from form-encoded request", async (t) => { + const result = await formEncodedToJf2({ h: "entry", content: "I+ate+a+cheese+sandwich,+which+was+nice.", category: ["foo", "bar"],