Skip to content

Commit 508ce0b

Browse files
committed
fix: reduce extractMetadata cyclomatic complexity, pin deps to exact versions
1 parent 4a3f431 commit 508ce0b

2 files changed

Lines changed: 15 additions & 18 deletions

File tree

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@
4949
},
5050
"dependencies": {
5151
"@modelcontextprotocol/sdk": "1.29.0",
52-
"@mozilla/readability": "^0.6.0",
52+
"@mozilla/readability": "0.6.0",
5353
"@types/cors": "^2.8.19",
5454
"@types/express": "^5.0.6",
5555
"cors": "^2.8.6",
5656
"express": "^5.2.1",
5757
"express-rate-limit": "^8.5.2",
58-
"linkedom": "^0.18.13",
58+
"linkedom": "0.18.13",
5959
"node-html-markdown": "^2.0.0",
6060
"node-html-parser": "^6.1.13",
6161
"undici": "7.28.0"
6262
},
6363
"devDependencies": {
64-
"@types/mozilla__readability": "^0.4.2",
64+
"@types/mozilla__readability": "0.4.2",
6565
"@types/node": "22.20.1",
6666
"@types/supertest": "^7.2.0",
6767
"@typescript-eslint/eslint-plugin": "8.63.0",

src/url-reader.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,33 +199,30 @@ function getMeta(doc: Document, name: string): string | undefined {
199199
return el?.getAttribute("content")?.trim() || undefined;
200200
}
201201

202+
function firstMeta(doc: Document, names: string[]): string | undefined {
203+
for (const name of names) {
204+
const value = getMeta(doc, name);
205+
if (value) return value;
206+
}
207+
return undefined;
208+
}
209+
202210
export function extractMetadata(html: string, url: string): PageMetadata {
203211
const { document } = parseHTML(html);
204212
const result: PageMetadata = {};
205213

206-
const title = getMeta(document, "og:title")
207-
|| getMeta(document, "twitter:title")
214+
const title = firstMeta(document, ["og:title", "twitter:title"])
208215
|| document.querySelector("title")?.textContent?.trim()
209216
|| undefined;
210217
if (title) result.title = title;
211218

212-
const author = getMeta(document, "author")
213-
|| getMeta(document, "article:author")
214-
|| getMeta(document, "og:article:author")
215-
|| undefined;
219+
const author = firstMeta(document, ["author", "article:author", "og:article:author"]);
216220
if (author) result.author = author;
217221

218-
const publishedDate = getMeta(document, "article:published_time")
219-
|| getMeta(document, "og:article:published_time")
220-
|| getMeta(document, "date")
221-
|| getMeta(document, "pubdate")
222-
|| undefined;
222+
const publishedDate = firstMeta(document, ["article:published_time", "og:article:published_time", "date", "pubdate"]);
223223
if (publishedDate) result.publishedDate = publishedDate;
224224

225-
const description = getMeta(document, "description")
226-
|| getMeta(document, "og:description")
227-
|| getMeta(document, "twitter:description")
228-
|| undefined;
225+
const description = firstMeta(document, ["description", "og:description", "twitter:description"]);
229226
if (description) result.description = description;
230227

231228
const siteName = getMeta(document, "og:site_name") || undefined;

0 commit comments

Comments
 (0)