From 190aeba6d47fbbb041028beb124603ef3b181399 Mon Sep 17 00:00:00 2001 From: Oscar Otero Date: Mon, 8 Jan 2024 15:17:35 +0100 Subject: [PATCH] Added function support for metas & feed --- CHANGELOG.md | 3 +++ core/utils/data_values.ts | 4 ++++ plugins/feed.ts | 12 ++++++------ plugins/metas.ts | 26 +++++++++++++------------- tests/__snapshots__/feed.test.ts.snap | 20 ++++++++++---------- tests/__snapshots__/metas.test.ts.snap | 6 +++--- tests/assets/metas/_data.js | 16 ++++++++++++++++ tests/assets/metas/_data.yml | 14 -------------- tests/feed.test.ts | 1 + 9 files changed, 56 insertions(+), 46 deletions(-) create mode 100644 tests/assets/metas/_data.js delete mode 100644 tests/assets/metas/_data.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 78a296a7a..47906e24b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project try to adheres to [Semantic Versioning](https://semver.org/). Go to the `v1` branch to see the changelog of Lume 1. ## [Unreleased] +### Added +- `metas` & `feed` plugins: Suppport for functions to configure the data fields. + ### Fixed - esbuild plugin: Fix support for subextensions added to JSX files. For example: `file.client.jsx`. diff --git a/core/utils/data_values.ts b/core/utils/data_values.ts index 58700d2c2..650ad905d 100644 --- a/core/utils/data_values.ts +++ b/core/utils/data_values.ts @@ -31,5 +31,9 @@ export function getDataValue(data: Partial, value?: unknown) { } } + if (typeof value === "function") { + return value(data); + } + return value; } diff --git a/plugins/feed.ts b/plugins/feed.ts index df03b17c4..12cee2d3e 100644 --- a/plugins/feed.ts +++ b/plugins/feed.ts @@ -53,22 +53,22 @@ export interface FeedInfoOptions { export interface FeedItemOptions { /** The item title */ - title?: string; + title?: string | ((data: Data) => string | undefined); /** The item description */ - description?: string; + description?: string | ((data: Data) => string | undefined); /** The item published date */ - published?: string; + published?: string | ((data: Data) => Date | undefined); /** The item updated date */ - updated?: string; + updated?: string | ((data: Data) => Date | undefined); /** The item content */ - content?: string; + content?: string | ((data: Data) => string | undefined); /** The item language */ - lang?: string; + lang?: string | ((data: Data) => string | undefined); } export const defaults: Options = { diff --git a/plugins/metas.ts b/plugins/metas.ts index 9029d3abe..05dd366e6 100644 --- a/plugins/metas.ts +++ b/plugins/metas.ts @@ -3,7 +3,7 @@ import { getCurrentVersion } from "../core/utils/lume_version.ts"; import { getDataValue } from "../core/utils/data_values.ts"; import type Site from "../core/site.ts"; -import type { Page } from "../core/file.ts"; +import type { Data, Page } from "../core/file.ts"; export interface Options { /** The list extensions this plugin applies to */ @@ -15,40 +15,40 @@ export interface Options { export interface MetaData { /** The type of the site default is website */ - type?: string; + type?: string | ((data: Data) => string | undefined); /** The name of the site */ - site?: string; + site?: string | ((data: Data) => string | undefined); /** The title of the page */ - title?: string; + title?: string | ((data: Data) => string | undefined); /** The page language */ - lang?: string; + lang?: string | ((data: Data) => string | undefined); /** The description of the page */ - description?: string; + description?: string | ((data: Data) => string | undefined); /** The image of the page */ - image?: string; + image?: string | ((data: Data) => string | undefined); /** The icon of the site */ - icon?: string; + icon?: string | ((data: Data) => string | undefined); /** The page keywords */ - keywords?: string[]; + keywords?: string[] | ((data: Data) => string[] | undefined); /** The twitter username */ - twitter?: string; + twitter?: string | ((data: Data) => string | undefined); /** The color theme */ - color?: string; + color?: string | ((data: Data) => string | undefined); /** Robots configuration (Boolean to enable/disable, String for a custom value) */ - robots?: string | boolean; + robots?: string | boolean | ((data: Data) => string | boolean | undefined); /** Whether include the generator or not (Boolean to enable/disable, String for a custom value) */ - generator?: string | boolean; + generator?: string | boolean | ((data: Data) => string | boolean | undefined); } const defaults: Options = { diff --git a/tests/__snapshots__/feed.test.ts.snap b/tests/__snapshots__/feed.test.ts.snap index d7aadbf55..b9ca90f87 100644 --- a/tests/__snapshots__/feed.test.ts.snap +++ b/tests/__snapshots__/feed.test.ts.snap @@ -136,10 +136,10 @@ snapshot[`RSS plugin 2`] = `[]`; snapshot[`RSS plugin 3`] = ` [ { - content: '{"version":"https://jsonfeed.org/version/1","title":"My RSS Feed","home_page_url":"https://example.com/","feed_url":"https://example.com/feed.json","description":"","items":[{"id":"https://example.com/pages/new-name/page7/","url":"https://example.com/pages/new-name/page7/","content_html":"Content of Page 7","date_published":"Sun, 02 Jan 2022 00:00:00 GMT","date_modified":"Sun, 02 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page6/","url":"https://example.com/pages/page6/","title":"Page 6","content_html":"

Content of Page 6

\\\\n","date_published":"Sat, 01 Jan 2022 00:00:00 GMT","date_modified":"Sat, 01 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page4/","url":"https://example.com/pages/page4/","title":"Page 4","content_html":"Content of Page 4 in Overrided site name, from the file /pages/2021-01-02-18-32_page4.page.ts","date_published":"Sat, 02 Jan 2021 18:32:00 GMT","date_modified":"Sat, 02 Jan 2021 18:32:00 GMT"},{"id":"https://example.com/overrided-page2/","url":"https://example.com/overrided-page2/","title":"Page 2","content_html":"Content of Page 2","date_published":"Sun, 21 Jun 2020 00:00:00 GMT","date_modified":"Sun, 21 Jun 2020 00:00:00 GMT"},{"id":"https://example.com/page5/","url":"https://example.com/page5/","title":"Page 5","content_html":"Content of Page 5","date_published":"Thu, 21 Jun 1979 23:45:00 GMT","date_modified":"Thu, 21 Jun 1979 23:45:00 GMT"}]}', + content: '{"version":"https://jsonfeed.org/version/1","title":"My RSS Feed","home_page_url":"https://example.com/","feed_url":"https://example.com/feed.json","description":"","items":[{"id":"https://example.com/pages/new-name/page7/","url":"https://example.com/pages/new-name/page7/","content_html":"Content of Page 7","date_published":"Sun, 02 Jan 2022 00:00:00 GMT","date_modified":"Sun, 02 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page6/","url":"https://example.com/pages/page6/","title":"PAGE 6","content_html":"

Content of Page 6

\\\\n","date_published":"Sat, 01 Jan 2022 00:00:00 GMT","date_modified":"Sat, 01 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page4/","url":"https://example.com/pages/page4/","title":"PAGE 4","content_html":"Content of Page 4 in Overrided site name, from the file /pages/2021-01-02-18-32_page4.page.ts","date_published":"Sat, 02 Jan 2021 18:32:00 GMT","date_modified":"Sat, 02 Jan 2021 18:32:00 GMT"},{"id":"https://example.com/overrided-page2/","url":"https://example.com/overrided-page2/","title":"PAGE 2","content_html":"Content of Page 2","date_published":"Sun, 21 Jun 2020 00:00:00 GMT","date_modified":"Sun, 21 Jun 2020 00:00:00 GMT"},{"id":"https://example.com/page5/","url":"https://example.com/page5/","title":"PAGE 5","content_html":"Content of Page 5","date_published":"Thu, 21 Jun 1979 23:45:00 GMT","date_modified":"Thu, 21 Jun 1979 23:45:00 GMT"}]}', data: { basename: "feed", - content: '{"version":"https://jsonfeed.org/version/1","title":"My RSS Feed","home_page_url":"https://example.com/","feed_url":"https://example.com/feed.json","description":"","items":[{"id":"https://example.com/pages/new-name/page7/","url":"https://example.com/pages/new-name/page7/","content_html":"Content of Page 7","date_published":"Sun, 02 Jan 2022 00:00:00 GMT","date_modified":"Sun, 02 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page6/","url":"https://example.com/pages/page6/","title":"Page 6","content_html":"

Content of Page 6

\\\\n","date_published":"Sat, 01 Jan 2022 00:00:00 GMT","date_modified":"Sat, 01 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page4/","url":"https://example.com/pages/page4/","title":"Page 4","content_html":"Content of Page 4 in Overrided site name, from the file /pages/2021-01-02-18-32_page4.page.ts","date_published":"Sat, 02 Jan 2021 18:32:00 GMT","date_modified":"Sat, 02 Jan 2021 18:32:00 GMT"},{"id":"https://example.com/overrided-page2/","url":"https://example.com/overrided-page2/","title":"Page 2","content_html":"Content of Page 2","date_published":"Sun, 21 Jun 2020 00:00:00 GMT","date_modified":"Sun, 21 Jun 2020 00:00:00 GMT"},{"id":"https://example.com/page5/","url":"https://example.com/page5/","title":"Page 5","content_html":"Content of Page 5","date_published":"Thu, 21 Jun 1979 23:45:00 GMT","date_modified":"Thu, 21 Jun 1979 23:45:00 GMT"}]}', + content: '{"version":"https://jsonfeed.org/version/1","title":"My RSS Feed","home_page_url":"https://example.com/","feed_url":"https://example.com/feed.json","description":"","items":[{"id":"https://example.com/pages/new-name/page7/","url":"https://example.com/pages/new-name/page7/","content_html":"Content of Page 7","date_published":"Sun, 02 Jan 2022 00:00:00 GMT","date_modified":"Sun, 02 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page6/","url":"https://example.com/pages/page6/","title":"PAGE 6","content_html":"

Content of Page 6

\\\\n","date_published":"Sat, 01 Jan 2022 00:00:00 GMT","date_modified":"Sat, 01 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page4/","url":"https://example.com/pages/page4/","title":"PAGE 4","content_html":"Content of Page 4 in Overrided site name, from the file /pages/2021-01-02-18-32_page4.page.ts","date_published":"Sat, 02 Jan 2021 18:32:00 GMT","date_modified":"Sat, 02 Jan 2021 18:32:00 GMT"},{"id":"https://example.com/overrided-page2/","url":"https://example.com/overrided-page2/","title":"PAGE 2","content_html":"Content of Page 2","date_published":"Sun, 21 Jun 2020 00:00:00 GMT","date_modified":"Sun, 21 Jun 2020 00:00:00 GMT"},{"id":"https://example.com/page5/","url":"https://example.com/page5/","title":"PAGE 5","content_html":"Content of Page 5","date_published":"Thu, 21 Jun 1979 23:45:00 GMT","date_modified":"Thu, 21 Jun 1979 23:45:00 GMT"}]}', page: [ "src", "data", @@ -172,7 +172,7 @@ snapshot[`RSS plugin 3`] = ` 2022-01-02T00:00:00.000Z - Page 6 + PAGE 6 https://example.com/pages/page6/ https://example.com/pages/page6/ @@ -183,7 +183,7 @@ snapshot[`RSS plugin 3`] = ` 2022-01-01T00:00:00.000Z - Page 4 + PAGE 4 https://example.com/pages/page4/ https://example.com/pages/page4/ @@ -191,7 +191,7 @@ snapshot[`RSS plugin 3`] = ` 2021-01-02T18:32:00.000Z - Page 2 + PAGE 2 https://example.com/overrided-page2/ https://example.com/overrided-page2/ @@ -199,7 +199,7 @@ snapshot[`RSS plugin 3`] = ` 2020-06-21T00:00:00.000Z - Page 5 + PAGE 5 https://example.com/page5/ https://example.com/page5/ @@ -228,7 +228,7 @@ snapshot[`RSS plugin 3`] = ` 2022-01-02T00:00:00.000Z - Page 6 + PAGE 6 https://example.com/pages/page6/ https://example.com/pages/page6/ @@ -239,7 +239,7 @@ snapshot[`RSS plugin 3`] = ` 2022-01-01T00:00:00.000Z - Page 4 + PAGE 4 https://example.com/pages/page4/ https://example.com/pages/page4/ @@ -247,7 +247,7 @@ snapshot[`RSS plugin 3`] = ` 2021-01-02T18:32:00.000Z - Page 2 + PAGE 2 https://example.com/overrided-page2/ https://example.com/overrided-page2/ @@ -255,7 +255,7 @@ snapshot[`RSS plugin 3`] = ` 2020-06-21T00:00:00.000Z - Page 5 + PAGE 5 https://example.com/page5/ https://example.com/page5/ diff --git a/tests/__snapshots__/metas.test.ts.snap b/tests/__snapshots__/metas.test.ts.snap index bf95f88e9..d4dbc6dc9 100644 --- a/tests/__snapshots__/metas.test.ts.snap +++ b/tests/__snapshots__/metas.test.ts.snap @@ -104,7 +104,7 @@ snapshot[`metas plugin 1`] = ` ], src: [ "/", - "/_data.yml", + "/_data.js", "/page-1.vto", "/page-2.vto", "/page-3.md", @@ -272,12 +272,12 @@ snapshot[`metas plugin 3`] = ` - + - + diff --git a/tests/assets/metas/_data.js b/tests/assets/metas/_data.js new file mode 100644 index 000000000..a650226d7 --- /dev/null +++ b/tests/assets/metas/_data.js @@ -0,0 +1,16 @@ +export const metas = { + site: "My site", + title: "My title", + description: (data) => data.excerpt?.toUpperCase(), + image: "/my-image.png", + icon: "/my-icon.png", + robots: false, + keywords: [ + "one", + "two", + ], + twitter: "@myUser", + lang: "gl", + color: "black", + generator: "Lume testing", +}; diff --git a/tests/assets/metas/_data.yml b/tests/assets/metas/_data.yml deleted file mode 100644 index dd5918cde..000000000 --- a/tests/assets/metas/_data.yml +++ /dev/null @@ -1,14 +0,0 @@ -metas: - site: My site - title: My title - description: =excerpt - image: /my-image.png - icon: /my-icon.png - robots: false - keywords: - - one - - two - twitter: "@myUser" - lang: gl - color: black - generator: "Lume testing" \ No newline at end of file diff --git a/tests/feed.test.ts b/tests/feed.test.ts index 47d747ccd..2283ddd6f 100644 --- a/tests/feed.test.ts +++ b/tests/feed.test.ts @@ -15,6 +15,7 @@ Deno.test("RSS plugin", async (t) => { generator: "https://lume.land", }, items: { + title: (data) => data.title?.toUpperCase(), updated: "=date", }, }),