MCPcopy
hub / github.com/marktext/marktext / getPageTitle

Function getPageTitle

packages/muya/src/utils/paste.ts:8–35  ·  view source on GitHub ↗
(url: string)

Source from the content-addressed store, hash-verified

6export const isOnline = () => navigator.onLine === true;
7
8export async function getPageTitle(url: string) {
9 // No need to request the title when it's not url.
10 if (!url.startsWith('http'))
11 return '';
12
13 // No need to request the title when off line.
14 if (!isOnline())
15 return '';
16
17 try {
18 const res = await fetch(url, { method: 'GET', mode: 'cors' });
19 const contentType = res.headers.get('content-type');
20
21 if (res.status !== 200 || !contentType || !/text\/html/i.test(contentType))
22 return '';
23
24 // The response is HTML — read it as text and pluck `<title>`.
25 // Pre-fix this called `res.json()`, which always threw and made
26 // the helper silently return '' (marktext 141d25d8 / #1344).
27 const body = await res.text();
28 const match = body.match(/<title>([\s\S]*?)<\/title>/i);
29
30 return match && match[1] ? match[1].trim() : '';
31 }
32 catch {
33 return '';
34 }
35}
36
37export async function normalizePastedHTML(html: string) {
38 // Only extract the `body.innerHTML` when the `html` is a full HTML Document.

Callers 2

normalizePastedHTMLFunction · 0.70

Calls 3

getMethod · 0.80
textMethod · 0.80
isOnlineFunction · 0.70

Tested by

no test coverage detected