From 90b08ebde6617ddd609a361b0d3797ccb7b24907 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Thu, 24 Apr 2025 15:10:20 -0400 Subject: [PATCH] refactor - cleanup code + types --- .../website/listing/website-listing-read.ts | 2 +- .../types/website/util/discover-meta.ts | 69 ++++--------------- 2 files changed, 16 insertions(+), 55 deletions(-) diff --git a/src/project/types/website/listing/website-listing-read.ts b/src/project/types/website/listing/website-listing-read.ts index 40e454b1dd6..c8aacd50096 100644 --- a/src/project/types/website/listing/website-listing-read.ts +++ b/src/project/types/website/listing/website-listing-read.ts @@ -1159,7 +1159,7 @@ async function listItemFromFile( ) : []; - const readingContext = target?.markdown + const readingContext = target?.markdown?.markdown ? estimateReadingTimeMinutes(target.markdown.markdown) : undefined; let readingtime = undefined; diff --git a/src/project/types/website/util/discover-meta.ts b/src/project/types/website/util/discover-meta.ts index a69987f1cf5..caf88481e17 100644 --- a/src/project/types/website/util/discover-meta.ts +++ b/src/project/types/website/util/discover-meta.ts @@ -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: @@ -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( @@ -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( @@ -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) }; }