1
1
/*
2
2
* discover-meta.ts
3
3
*
4
- * Copyright (C) 2020-2022 Posit Software, PBC
4
+ * Copyright (C) 2020-2025 Posit Software, PBC
5
5
*/
6
6
7
7
import { Document , Element } from "deno_dom/deno-dom-wasm-noinit.ts" ;
8
-
9
8
import { getDecodedAttribute } from "../../../../core/html.ts" ;
10
9
11
10
// Image discovery happens by either:
@@ -16,15 +15,7 @@ import { getDecodedAttribute } from "../../../../core/html.ts";
16
15
const kPreviewImgClass = "preview-image" ;
17
16
const kNamedFilePattern =
18
17
"(.*?(?:preview|feature|cover|thumbnail).*?(?:\\.png|\\.gif|\\.jpg|\\.jpeg|\\.webp))" ;
19
- const kPreviewClassPattern =
20
- `!\\[[^\\]]*\\]\\((.*?)(?:\\".*\\")?\\)\\{[^\\|]*\\.${ kPreviewImgClass } [\\s\\}]+` ;
21
- const kMdNamedImagePattern =
22
- `!\\[[^\\]]*\\]\\(${ kNamedFilePattern } (?:\\".*\\")?\\)(?:\\{[^\\|]*\.*[\\s\\}]+)?` ;
23
-
24
18
const kNamedFileRegex = RegExp ( kNamedFilePattern , "l" ) ;
25
- const kMdPreviewClassRegex = RegExp ( kPreviewClassPattern , "l" ) ;
26
- const kMdNamedImageRegex = RegExp ( kMdNamedImagePattern , "l" ) ;
27
- const kMarkdownImg = / ! \[ [ ^ \] ] * \] \( ( .* ?) (?: \" .* \" ) ? \) (?: \{ (?: [ ^ \| ] * ) \} ) ? / ;
28
19
29
20
export function findDescription ( doc : Document ) : string | undefined {
30
21
const paras = doc . querySelectorAll (
@@ -43,20 +34,16 @@ export function findPreviewImg(
43
34
doc : Document ,
44
35
) : { src : string ; alt ?: string } | undefined {
45
36
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
+ } ;
60
47
}
61
48
62
49
export function findPreviewImgEl (
@@ -107,34 +94,8 @@ export function findPreviewImgEl(
107
94
// So 200 is a good middle ground estimate.
108
95
const kWpm = 200 ;
109
96
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 ) } ;
140
101
}
0 commit comments