11/*
22 * discover-meta.ts
33 *
4- * Copyright (C) 2020-2022 Posit Software, PBC
4+ * Copyright (C) 2020-2025 Posit Software, PBC
55 */
66
77import { Document , Element } from "deno_dom/deno-dom-wasm-noinit.ts" ;
8-
98import { getDecodedAttribute } from "../../../../core/html.ts" ;
109
1110// Image discovery happens by either:
@@ -16,15 +15,7 @@ import { getDecodedAttribute } from "../../../../core/html.ts";
1615const kPreviewImgClass = "preview-image" ;
1716const kNamedFilePattern =
1817 "(.*?(?:preview|feature|cover|thumbnail).*?(?:\\.png|\\.gif|\\.jpg|\\.jpeg|\\.webp))" ;
19- const kPreviewClassPattern =
20- `!\\[[^\\]]*\\]\\((.*?)(?:\\".*\\")?\\)\\{[^\\|]*\\.${ kPreviewImgClass } [\\s\\}]+` ;
21- const kMdNamedImagePattern =
22- `!\\[[^\\]]*\\]\\(${ kNamedFilePattern } (?:\\".*\\")?\\)(?:\\{[^\\|]*\.*[\\s\\}]+)?` ;
23-
2418const kNamedFileRegex = RegExp ( kNamedFilePattern , "l" ) ;
25- const kMdPreviewClassRegex = RegExp ( kPreviewClassPattern , "l" ) ;
26- const kMdNamedImageRegex = RegExp ( kMdNamedImagePattern , "l" ) ;
27- const kMarkdownImg = / ! \[ [ ^ \] ] * \] \( ( .* ?) (?: \" .* \" ) ? \) (?: \{ (?: [ ^ \| ] * ) \} ) ? / ;
2819
2920export 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
6249export function findPreviewImgEl (
@@ -107,34 +94,8 @@ export function findPreviewImgEl(
10794// So 200 is a good middle ground estimate.
10895const kWpm = 200 ;
10996export 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