Skip to content

Commit

Permalink
Fix #683
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Oct 27, 2024
1 parent 036b879 commit 4767b68
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
- New plugin: `brotli` to compress files. [#681]
- New plugin: `gzip` to compress files. [#680]
- New `precompress` middleware, to serve precompressed files. [#664]
- Allow to specify fallbacks for `metas` and `feed` plugins [#683].

### Fixed
- Nav plugin: Breadcrumb with urls with CJK characters.
Expand Down Expand Up @@ -570,6 +571,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
[#679]: https://github.com/lumeland/lume/issues/679
[#680]: https://github.com/lumeland/lume/issues/680
[#681]: https://github.com/lumeland/lume/issues/681
[#683]: https://github.com/lumeland/lume/issues/683

[Unreleased]: https://github.com/lumeland/lume/compare/v2.3.3...HEAD
[2.3.3]: https://github.com/lumeland/lume/compare/v2.3.2...v2.3.3
Expand Down
74 changes: 52 additions & 22 deletions core/utils/data_values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,74 @@ import type { Data } from "../file.ts";
export function getDataValue(data: Partial<Data>, value?: unknown) {
// Get the value from the page data
if (typeof value === "string") {
if (value.startsWith("=")) {
const key = value.slice(1);

if (!key.includes(".")) {
return data[key];
}

const keys = key.split(".");
let val = data;
for (const key of keys) {
val = val?.[key];
}
return val;
return searchValue(data, value);
}

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

return value;
}

function searchValue(data: Partial<Data>, value: string): unknown {
if (!value) {
return;
}

if (value.startsWith("=")) {
let key = value.slice(1);
[key, value] = parseFallback(key);

if (!key.includes(".")) {
return data[key] ?? searchValue(data, value);
}

if (value.startsWith("$")) {
return queryCss(value, data.page?.document);
const keys = key.split(".");
let val = data;
for (const key of keys) {
val = val?.[key];
}
return val ?? searchValue(data, value);
}

if (typeof value === "function") {
return value(data);
if (value.startsWith("$")) {
let selector = value.slice(1);
[selector, value] = parseFallback(selector);

return queryCss(selector, data.page?.document) ?? searchValue(data, value);
}

return value;
}

function queryCss(value: string, document?: Document) {
// https://regexr.com/7qnot
const checkForAttrPattern = /^\$(.+)\s+(?:attr\(([\w\-]+)\))$/;
const checkResult = value.match(checkForAttrPattern);
function parseFallback(key: string): [string, string] {
const fallback = key.indexOf("||");

if (fallback !== -1) {
return [
key.slice(0, fallback).trim(),
key.slice(fallback + 2).trim(),
];
}

return [
key,
"",
];
}

// https://regexr.com/7qnot
const checkForAttrPattern = /^(.+)\s+(?:attr\(([\w\-]+)\))$/;

function queryCss(query: string, document?: Document) {
const checkResult = query.match(checkForAttrPattern);

const hasAttr = checkResult?.[0];
if (hasAttr) {
const [_, query, name] = checkResult;
return document?.querySelector(query)?.getAttribute(name);
}

const query = value.slice(1);
return document?.querySelector(query)?.innerHTML;
}
8 changes: 3 additions & 5 deletions tests/__snapshots__/feed.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,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/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","items":[{"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/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","items":[{"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 All @@ -142,7 +142,6 @@ snapshot[`RSS plugin 3`] = `
<title>My RSS Feed</title>
<link>https://example.com/</link>
<atom:link href="https://example.com/feed.rss" rel="self" type="application/rss+xml"/>
<description/>
<lastBuildDate>Wed, 01 Jan 2020 00:00:00 GMT</lastBuildDate>
<language>en</language>
<generator>https://lume.land</generator>
Expand All @@ -164,7 +163,6 @@ snapshot[`RSS plugin 3`] = `
<title>My RSS Feed</title>
<link>https://example.com/</link>
<atom:link href="https://example.com/feed.rss" rel="self" type="application/rss+xml"/>
<description/>
<lastBuildDate>Wed, 01 Jan 2020 00:00:00 GMT</lastBuildDate>
<language>en</language>
<generator>https://lume.land</generator>
Expand Down Expand Up @@ -225,7 +223,7 @@ snapshot[`RSS plugin 3`] = `
},
{
content: "<!DOCTYPE html>
Content of Page 5",
<html><head></head><body>Content of Page 5</body></html>",
data: {
basename: "page5",
children: "Content of Page 5",
Expand Down
4 changes: 2 additions & 2 deletions tests/feed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Deno.test("RSS plugin", async (t) => {
},
items: {
title: (data) => data.title?.toUpperCase(),
updated: "=date",
published: "=published",
updated: "=date || $.date",
published: "$.not-found || =published",
},
}),
);
Expand Down

0 comments on commit 4767b68

Please sign in to comment.