Skip to content

Commit

Permalink
remove some markdown & html syntax in feed and metas plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jan 9, 2025
1 parent 0455632 commit 3975ee7
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
- `theme.path` option of `code_highlight` plugin.

### Fixed
- Removed markdown syntax for some values in `metas`, `feed` and `json_ld`.
- `relative_urls` plugin with no pretty urls [#711].
- Updated deps: `sass`, `preact`, `xml`, `katex`, `jsr`, `pagefind`, `highlight.js`, `tailwindcss`, `magic-string`, `std`, `esbuild`, `liquid`, `markdown-it-attrs`, `unocss`, `vento`, `deno_dom`, `unocss`, `satori` and some icons.
- Several changes on `esbuild` plugin:
Expand Down
12 changes: 12 additions & 0 deletions core/utils/data_values.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { plainText } from "../../deps/remove-markdown.ts";

import type { Data } from "../file.ts";

/**
Expand All @@ -18,6 +20,16 @@ export function getDataValue(data: Partial<Data>, value?: unknown) {
return value;
}

export function getPlainDataValue(data: Partial<Data>, value?: unknown) {
const val = getDataValue(data, value);

if (typeof val === "string") {
return plainText(val);
}

return val;
}

function searchValue(data: Partial<Data>, value: string): unknown {
if (!value) {
return;
Expand Down
25 changes: 25 additions & 0 deletions deps/remove-markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import removeMarkdown from "npm:[email protected]";

export function plainText(md: string, options?: RemoveMarkdownOptions): string {
return removeMarkdown(md.replaceAll(/\s+/g, " ").trim(), options);
}

export interface RemoveMarkdownOptions {
/** strip list leaders (default: true) */
stripListLeaders?: boolean;

/** char to insert instead of stripped list leaders (default: '') */
listUnicodeChar?: string;

/** support GitHub-Flavored Markdown (default: true) */
gfm?: boolean;

/** replace images with alt-text, if present (default: true) */
useImgAltText: boolean;

abbr?: boolean;

/** replace links with URLs instead anchor text (default: false) */
replaceLinksWithURL?: boolean;
htmlTagsToSkip?: string[];
}
12 changes: 6 additions & 6 deletions plugins/feed.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getExtension } from "../core/utils/path.ts";
import { isPlainObject, merge } from "../core/utils/object.ts";
import { getCurrentVersion } from "../core/utils/lume_version.ts";
import { getDataValue } from "../core/utils/data_values.ts";
import { getDataValue, getPlainDataValue } from "../core/utils/data_values.ts";
import { cdata, stringify } from "../deps/xml.ts";
import { Page } from "../core/file.ts";
import { parseDate } from "../core/utils/date.ts";
Expand Down Expand Up @@ -170,8 +170,8 @@ export function feed(userOptions?: Options) {
const rootData = site.source.data.get("/") || {};

const feed: FeedData = {
title: getDataValue(rootData, info.title),
description: getDataValue(rootData, info.description),
title: getPlainDataValue(rootData, info.title),
description: getPlainDataValue(rootData, info.description),
published: getDataValue(rootData, info.published),
lang: getDataValue(rootData, info.lang),
url: site.url("", true),
Expand All @@ -189,9 +189,9 @@ export function feed(userOptions?: Options) {
: undefined;

return {
title: getDataValue(data, items.title),
title: getPlainDataValue(data, items.title),
url: site.url(data.url, true),
description: getDataValue(data, items.description),
description: getPlainDataValue(data, items.description),
author: getAuthor(data, items),
published: toDate(getDataValue(data, items.published)) ||
new Date(),
Expand Down Expand Up @@ -234,7 +234,7 @@ function getAuthor(
data: Partial<Data>,
info: FeedInfoOptions | FeedItemOptions,
): Author | undefined {
const name = getDataValue(data, info.authorName);
const name = getPlainDataValue(data, info.authorName);
const url = getDataValue(data, info.authorUrl);

if (name || url) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/json_ld.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isPlainObject, merge } from "../core/utils/object.ts";
import { getDataValue } from "../core/utils/data_values.ts";
import { getPlainDataValue } from "../core/utils/data_values.ts";

import type Site from "../core/site.ts";
import type { Page } from "../core/file.ts";
Expand Down Expand Up @@ -205,7 +205,7 @@ export function jsonLd(userOptions?: Options) {
// Recursive function to traverse and process JSON-LD data
function traverse(key: string | undefined, value: unknown): unknown {
if (typeof value === "string") {
const dataValue = getDataValue(data, value);
const dataValue = getPlainDataValue(data, value);
// Check if the value is a URL or ID that needs to be processed
if (
key &&
Expand Down
16 changes: 6 additions & 10 deletions plugins/metas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { merge } from "../core/utils/object.ts";
import { getCurrentVersion } from "../core/utils/lume_version.ts";
import { getDataValue } from "../core/utils/data_values.ts";
import { getDataValue, getPlainDataValue } from "../core/utils/data_values.ts";

import type Site from "../core/site.ts";
import type { Data, Page } from "../core/file.ts";
Expand Down Expand Up @@ -96,12 +96,12 @@ export function metas(userOptions?: Options) {
: undefined;

const type = getDataValue(data, main["type"]);
const site_name = getDataValue(data, main["site"]);
const site_name = getPlainDataValue(data, main["site"]);
const lang = getDataValue(data, main["lang"]);
const title = getDataValue(data, main["title"]);
const description = getDataValue(data, main["description"]);
const twitter = getDataValue(data, main["twitter"]);
const fediverse = getDataValue(data, main["fediverse"]);
const title = getPlainDataValue(data, main["title"]);
const description = getPlainDataValue(data, main["description"]);
const twitter = getPlainDataValue(data, main["twitter"]);
const fediverse = getPlainDataValue(data, main["fediverse"]);
const keywords = getDataValue(data, main["keywords"]);
const robots = getDataValue(data, main["robots"]);
const color = getDataValue(data, main["color"]);
Expand Down Expand Up @@ -181,10 +181,6 @@ function addMeta(
if (!content) {
return;
}
content = content
.replaceAll(/<[^>]*>/g, "")
.replaceAll(/\s+/g, " ")
.trim();

if (limit && content.length > limit) {
content = content.substring(0, limit - 1).trimEnd() + "…";
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/feed.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ snapshot[`RSS plugin 3`] = `
published: "1979-06-21T23:45:00.000Z",
search: [],
tags: "Array(0)",
title: "Page 5",
title: "Page **5**",
url: "/page5/",
},
src: {
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/feed/page5.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
title: Page 5
title: Page **5**
content: Content of Page 5
date: 1979-06-21T23:45:00.000Z
published: "1979-06-21T23:45:00.000Z"
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/metas/page-2.vto
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
metas:
title: Relative paths
title: Relative **paths**
description: Tests the use of relative path (to page.data.url) when filling out the og:image or og:icon URL
image: ./my-image.png
icon: ./my-icon.png
Expand Down

0 comments on commit 3975ee7

Please sign in to comment.