Skip to content

Commit

Permalink
Added function support for metas & feed
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jan 8, 2024
1 parent 1f85f63 commit 190aeba
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 46 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 4 additions & 0 deletions core/utils/data_values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@ export function getDataValue(data: Partial<Data>, value?: unknown) {
}
}

if (typeof value === "function") {
return value(data);
}

return value;
}
12 changes: 6 additions & 6 deletions plugins/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
26 changes: 13 additions & 13 deletions plugins/metas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 = {
Expand Down
20 changes: 10 additions & 10 deletions tests/__snapshots__/feed.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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":"<p>Content of Page 6</p>\\\\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":"<p>Content of Page 6</p>\\\\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":"<p>Content of Page 6</p>\\\\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":"<p>Content of Page 6</p>\\\\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",
Expand Down Expand Up @@ -172,7 +172,7 @@ snapshot[`RSS plugin 3`] = `
<atom:updated>2022-01-02T00:00:00.000Z</atom:updated>
</item>
<item>
<title>Page 6</title>
<title>PAGE 6</title>
<link>https://example.com/pages/page6/</link>
<guid isPermaLink="false">https://example.com/pages/page6/</guid>
<content:encoded>
Expand All @@ -183,23 +183,23 @@ snapshot[`RSS plugin 3`] = `
<atom:updated>2022-01-01T00:00:00.000Z</atom:updated>
</item>
<item>
<title>Page 4</title>
<title>PAGE 4</title>
<link>https://example.com/pages/page4/</link>
<guid isPermaLink="false">https://example.com/pages/page4/</guid>
<content:encoded><![CDATA[Content of Page 4 in Overrided site name, from the file /pages/2021-01-02-18-32_page4.page.ts]]></content:encoded>
<pubDate>Sat, 02 Jan 2021 18:32:00 GMT</pubDate>
<atom:updated>2021-01-02T18:32:00.000Z</atom:updated>
</item>
<item>
<title>Page 2</title>
<title>PAGE 2</title>
<link>https://example.com/overrided-page2/</link>
<guid isPermaLink="false">https://example.com/overrided-page2/</guid>
<content:encoded><![CDATA[Content of Page 2]]></content:encoded>
<pubDate>Sun, 21 Jun 2020 00:00:00 GMT</pubDate>
<atom:updated>2020-06-21T00:00:00.000Z</atom:updated>
</item>
<item>
<title>Page 5</title>
<title>PAGE 5</title>
<link>https://example.com/page5/</link>
<guid isPermaLink="false">https://example.com/page5/</guid>
<content:encoded><![CDATA[Content of Page 5]]></content:encoded>
Expand Down Expand Up @@ -228,7 +228,7 @@ snapshot[`RSS plugin 3`] = `
<atom:updated>2022-01-02T00:00:00.000Z</atom:updated>
</item>
<item>
<title>Page 6</title>
<title>PAGE 6</title>
<link>https://example.com/pages/page6/</link>
<guid isPermaLink="false">https://example.com/pages/page6/</guid>
<content:encoded>
Expand All @@ -239,23 +239,23 @@ snapshot[`RSS plugin 3`] = `
<atom:updated>2022-01-01T00:00:00.000Z</atom:updated>
</item>
<item>
<title>Page 4</title>
<title>PAGE 4</title>
<link>https://example.com/pages/page4/</link>
<guid isPermaLink="false">https://example.com/pages/page4/</guid>
<content:encoded><![CDATA[Content of Page 4 in Overrided site name, from the file /pages/2021-01-02-18-32_page4.page.ts]]></content:encoded>
<pubDate>Sat, 02 Jan 2021 18:32:00 GMT</pubDate>
<atom:updated>2021-01-02T18:32:00.000Z</atom:updated>
</item>
<item>
<title>Page 2</title>
<title>PAGE 2</title>
<link>https://example.com/overrided-page2/</link>
<guid isPermaLink="false">https://example.com/overrided-page2/</guid>
<content:encoded><![CDATA[Content of Page 2]]></content:encoded>
<pubDate>Sun, 21 Jun 2020 00:00:00 GMT</pubDate>
<atom:updated>2020-06-21T00:00:00.000Z</atom:updated>
</item>
<item>
<title>Page 5</title>
<title>PAGE 5</title>
<link>https://example.com/page5/</link>
<guid isPermaLink="false">https://example.com/page5/</guid>
<content:encoded><![CDATA[Content of Page 5]]></content:encoded>
Expand Down
6 changes: 3 additions & 3 deletions tests/__snapshots__/metas.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ snapshot[`metas plugin 1`] = `
],
src: [
"/",
"/_data.yml",
"/_data.js",
"/page-1.vto",
"/page-2.vto",
"/page-3.md",
Expand Down Expand Up @@ -272,12 +272,12 @@ snapshot[`metas plugin 3`] = `
<meta property="og:site_name" content="My site">
<meta property="og:locale" content="gl">
<meta property="og:title" content="Title from page data">
<meta property="og:description" content="This is page excerpt will be used as meta description.">
<meta property="og:description" content="THIS IS PAGE EXCERPT WILL BE USED AS META DESCRIPTION.">
<meta property="og:url" content="http://localhost/page-3/">
<meta property="og:image" content="http://localhost/page-3/use-cover-as-meta-image.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@myUser">
<meta name="description" content="This is page excerpt will be used as meta description.">
<meta name="description" content="THIS IS PAGE EXCERPT WILL BE USED AS META DESCRIPTION.">
<meta name="keywords" content="one, two">
<meta name="robots" content="this robots will be overrided">
<meta name="theme-color" content="black">
Expand Down
16 changes: 16 additions & 0 deletions tests/assets/metas/_data.js
Original file line number Diff line number Diff line change
@@ -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",
};
14 changes: 0 additions & 14 deletions tests/assets/metas/_data.yml

This file was deleted.

1 change: 1 addition & 0 deletions tests/feed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Deno.test("RSS plugin", async (t) => {
generator: "https://lume.land",
},
items: {
title: (data) => data.title?.toUpperCase(),
updated: "=date",
},
}),
Expand Down

0 comments on commit 190aeba

Please sign in to comment.