Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/project/types/website/listing/website-listing-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ async function listItemFromFile(
)
: [];

const readingContext = target?.markdown
const readingContext = target?.markdown?.markdown
? estimateReadingTimeMinutes(target.markdown.markdown)
: undefined;
let readingtime = undefined;
Expand Down
69 changes: 15 additions & 54 deletions src/project/types/website/util/discover-meta.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/*
* discover-meta.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
* Copyright (C) 2020-2025 Posit Software, PBC
*/

import { Document, Element } from "deno_dom/deno-dom-wasm-noinit.ts";

import { getDecodedAttribute } from "../../../../core/html.ts";

// Image discovery happens by either:
Expand All @@ -16,15 +15,7 @@ import { getDecodedAttribute } from "../../../../core/html.ts";
const kPreviewImgClass = "preview-image";
const kNamedFilePattern =
"(.*?(?:preview|feature|cover|thumbnail).*?(?:\\.png|\\.gif|\\.jpg|\\.jpeg|\\.webp))";
const kPreviewClassPattern =
`!\\[[^\\]]*\\]\\((.*?)(?:\\".*\\")?\\)\\{[^\\|]*\\.${kPreviewImgClass}[\\s\\}]+`;
const kMdNamedImagePattern =
`!\\[[^\\]]*\\]\\(${kNamedFilePattern}(?:\\".*\\")?\\)(?:\\{[^\\|]*\.*[\\s\\}]+)?`;

const kNamedFileRegex = RegExp(kNamedFilePattern, "l");
const kMdPreviewClassRegex = RegExp(kPreviewClassPattern, "l");
const kMdNamedImageRegex = RegExp(kMdNamedImagePattern, "l");
const kMarkdownImg = /!\[[^\]]*\]\((.*?)(?:\".*\")?\)(?:\{(?:[^\|]*)\})?/;

export function findDescription(doc: Document): string | undefined {
const paras = doc.querySelectorAll(
Expand All @@ -43,20 +34,16 @@ export function findPreviewImg(
doc: Document,
): { src: string; alt?: string } | undefined {
const imgEl = findPreviewImgEl(doc);
if (imgEl) {
const src = getDecodedAttribute(imgEl, "src");
const alt = getDecodedAttribute(imgEl, "alt");
if (src !== null) {
return {
src,
alt: alt ?? undefined,
};
} else {
return undefined;
}
} else {
return undefined;
}
if (!imgEl) return undefined;

const src = getDecodedAttribute(imgEl, "src");
if (src === null) return undefined;

const alt = getDecodedAttribute(imgEl, "alt");
return {
src,
alt: alt ?? undefined,
};
}

export function findPreviewImgEl(
Expand Down Expand Up @@ -107,34 +94,8 @@ export function findPreviewImgEl(
// So 200 is a good middle ground estimate.
const kWpm = 200;
export function estimateReadingTimeMinutes(
markdown?: string,
): { wordCount: number; readingTime: number } | undefined {
if (markdown) {
const wordCount = markdown.split(" ").length;
return { wordCount, readingTime: Math.ceil(wordCount / kWpm) };
}
return undefined;
}

export function findPreviewImgMd(markdown?: string): string | undefined {
if (markdown) {
// Look for an explictly tagged image
const explicitMatch = markdown.match(kMdPreviewClassRegex);
if (explicitMatch) {
return explicitMatch[1];
}

// Look for an image with a 'magic' name
const fileNameMatch = markdown.match(kMdNamedImageRegex);
if (fileNameMatch) {
return fileNameMatch[1];
}

// Otherwise select the first image
const match = markdown.match(kMarkdownImg);
if (match) {
return match[1];
}
}
return undefined;
markdown: string,
): { wordCount: number; readingTime: number } {
const wordCount = markdown.split(" ").length;
return { wordCount, readingTime: Math.ceil(wordCount / kWpm) };
}
Loading