diff --git a/eslint.config.mjs b/eslint.config.mjs index a80b073..20f8f5f 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -11,13 +11,6 @@ export default [ })), // Obsidian plugin rules (v0.3.0+) ...obsidianmd.configs.recommended, - // converter.ts uses DOMParser documents, not Obsidian DOM - { - files: ["src/converter.ts"], - rules: { - "obsidianmd/prefer-create-el": "off", - }, - }, // Project-specific config { files: ["src/**/*.ts"], diff --git a/src/converter.ts b/src/converter.ts index 4d6efd3..b5a3ce3 100644 --- a/src/converter.ts +++ b/src/converter.ts @@ -9,29 +9,16 @@ function isSubstackImageUrl(url: string): boolean { const VIDEO_PLACEHOLDER_RE = /VIDEOEMBED([a-f0-9-]{36})ENDVIDEO/g; function replaceVideoDivs(html: string, downloadedUrls: Set): string { - const parser = new DOMParser(); - const doc = parser.parseFromString(html, 'text/html'); - - for (const div of Array.from(doc.querySelectorAll('div[id^="media-"]'))) { - const id = div.getAttribute('id') ?? ''; - if (id.length !== 42) continue; - const videoId = id.replace('media-', ''); - const videoUrl = `https://substack.com/api/v1/video/upload/${videoId}/src`; - // eslint-disable-next-line obsidianmd/prefer-create-el -- DOMParser document, not Obsidian DOM - const p = doc.createElement('p'); - if (downloadedUrls.has(videoUrl)) { - p.textContent = `VIDEOEMBED${videoId}ENDVIDEO`; - } else { - // eslint-disable-next-line obsidianmd/prefer-create-el -- DOMParser document, not Obsidian DOM - const a = doc.createElement('a'); - a.href = videoUrl; - a.textContent = 'Video'; - p.appendChild(a); + return html.replace( + /]+id="media-([^"]{36})"[^>]*>[\s\S]*?<\/div>/g, + (_match, videoId: string) => { + const videoUrl = `https://substack.com/api/v1/video/upload/${videoId}/src`; + if (downloadedUrls.has(videoUrl)) { + return `

VIDEOEMBED${videoId}ENDVIDEO

`; + } + return `

Video

`; } - div.replaceWith(p); - } - - return doc.body.innerHTML; + ); } function restoreVideoWikilinks(md: string): string {