Skip to content

Commit 6aaa47a

Browse files
authored
Merge pull request #12619 from quarto-dev/refactor/discover-meta
cleanup code + types around discover-meta.ts
2 parents e45ab48 + 90b08eb commit 6aaa47a

File tree

2 files changed

+16
-55
lines changed

2 files changed

+16
-55
lines changed

src/project/types/website/listing/website-listing-read.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ async function listItemFromFile(
11591159
)
11601160
: [];
11611161

1162-
const readingContext = target?.markdown
1162+
const readingContext = target?.markdown?.markdown
11631163
? estimateReadingTimeMinutes(target.markdown.markdown)
11641164
: undefined;
11651165
let readingtime = undefined;

src/project/types/website/util/discover-meta.ts

+15-54
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/*
22
* discover-meta.ts
33
*
4-
* Copyright (C) 2020-2022 Posit Software, PBC
4+
* Copyright (C) 2020-2025 Posit Software, PBC
55
*/
66

77
import { Document, Element } from "deno_dom/deno-dom-wasm-noinit.ts";
8-
98
import { getDecodedAttribute } from "../../../../core/html.ts";
109

1110
// Image discovery happens by either:
@@ -16,15 +15,7 @@ import { getDecodedAttribute } from "../../../../core/html.ts";
1615
const kPreviewImgClass = "preview-image";
1716
const kNamedFilePattern =
1817
"(.*?(?:preview|feature|cover|thumbnail).*?(?:\\.png|\\.gif|\\.jpg|\\.jpeg|\\.webp))";
19-
const kPreviewClassPattern =
20-
`!\\[[^\\]]*\\]\\((.*?)(?:\\".*\\")?\\)\\{[^\\|]*\\.${kPreviewImgClass}[\\s\\}]+`;
21-
const kMdNamedImagePattern =
22-
`!\\[[^\\]]*\\]\\(${kNamedFilePattern}(?:\\".*\\")?\\)(?:\\{[^\\|]*\.*[\\s\\}]+)?`;
23-
2418
const kNamedFileRegex = RegExp(kNamedFilePattern, "l");
25-
const kMdPreviewClassRegex = RegExp(kPreviewClassPattern, "l");
26-
const kMdNamedImageRegex = RegExp(kMdNamedImagePattern, "l");
27-
const kMarkdownImg = /!\[[^\]]*\]\((.*?)(?:\".*\")?\)(?:\{(?:[^\|]*)\})?/;
2819

2920
export function findDescription(doc: Document): string | undefined {
3021
const paras = doc.querySelectorAll(
@@ -43,20 +34,16 @@ export function findPreviewImg(
4334
doc: Document,
4435
): { src: string; alt?: string } | undefined {
4536
const imgEl = findPreviewImgEl(doc);
46-
if (imgEl) {
47-
const src = getDecodedAttribute(imgEl, "src");
48-
const alt = getDecodedAttribute(imgEl, "alt");
49-
if (src !== null) {
50-
return {
51-
src,
52-
alt: alt ?? undefined,
53-
};
54-
} else {
55-
return undefined;
56-
}
57-
} else {
58-
return undefined;
59-
}
37+
if (!imgEl) return undefined;
38+
39+
const src = getDecodedAttribute(imgEl, "src");
40+
if (src === null) return undefined;
41+
42+
const alt = getDecodedAttribute(imgEl, "alt");
43+
return {
44+
src,
45+
alt: alt ?? undefined,
46+
};
6047
}
6148

6249
export function findPreviewImgEl(
@@ -107,34 +94,8 @@ export function findPreviewImgEl(
10794
// So 200 is a good middle ground estimate.
10895
const kWpm = 200;
10996
export function estimateReadingTimeMinutes(
110-
markdown?: string,
111-
): { wordCount: number; readingTime: number } | undefined {
112-
if (markdown) {
113-
const wordCount = markdown.split(" ").length;
114-
return { wordCount, readingTime: Math.ceil(wordCount / kWpm) };
115-
}
116-
return undefined;
117-
}
118-
119-
export function findPreviewImgMd(markdown?: string): string | undefined {
120-
if (markdown) {
121-
// Look for an explictly tagged image
122-
const explicitMatch = markdown.match(kMdPreviewClassRegex);
123-
if (explicitMatch) {
124-
return explicitMatch[1];
125-
}
126-
127-
// Look for an image with a 'magic' name
128-
const fileNameMatch = markdown.match(kMdNamedImageRegex);
129-
if (fileNameMatch) {
130-
return fileNameMatch[1];
131-
}
132-
133-
// Otherwise select the first image
134-
const match = markdown.match(kMarkdownImg);
135-
if (match) {
136-
return match[1];
137-
}
138-
}
139-
return undefined;
97+
markdown: string,
98+
): { wordCount: number; readingTime: number } {
99+
const wordCount = markdown.split(" ").length;
100+
return { wordCount, readingTime: Math.ceil(wordCount / kWpm) };
140101
}

0 commit comments

Comments
 (0)