diff --git a/src/util/extractSitemapMetaFromHtml.ts b/src/util/extractSitemapMetaFromHtml.ts index 7dc6e75..e4897c1 100644 --- a/src/util/extractSitemapMetaFromHtml.ts +++ b/src/util/extractSitemapMetaFromHtml.ts @@ -1,5 +1,6 @@ import { withSiteUrl } from 'nuxt-site-config-kit/urls' import { parseURL } from 'ufo' +import { tryUseNuxt } from '@nuxt/kit' import type { ResolvedSitemapUrl, SitemapUrl, VideoEntry } from '../runtime/types' export function extractSitemapMetaFromHtml(html: string, options?: { images?: boolean, videos?: boolean, lastmod?: boolean, alternatives?: boolean }) { @@ -12,7 +13,7 @@ export function extractSitemapMetaFromHtml(html: string, options?: { images?: bo if (mainMatch?.[1] && mainMatch[1].includes(']*>/gi + const imgRegex = /]*?\s)?src=["']((?!data:|blob:|file:)[^"']+?)["'][^>]*>/gi let match while ((match = imgRegex.exec(mainMatch[1])) !== null) { @@ -22,7 +23,7 @@ export function extractSitemapMetaFromHtml(html: string, options?: { images?: bo let url = match[1] // if the match is relative if (url.startsWith('/')) - url = withSiteUrl(url) + url = tryUseNuxt() ? withSiteUrl(url) : url images.add(url) } } diff --git a/test/unit/extractSitemapMetaFromHtml.test.ts b/test/unit/extractSitemapMetaFromHtml.test.ts index e6ebaeb..6505656 100644 --- a/test/unit/extractSitemapMetaFromHtml.test.ts +++ b/test/unit/extractSitemapMetaFromHtml.test.ts @@ -82,6 +82,18 @@ describe('extractSitemapMetaFromHtml', () => { ], } `) + + const html3 = `
` + const testcase3 = extractSitemapMetaFromHtml(html3) + expect(testcase3).toMatchInlineSnapshot(` + { + "images": [ + { + "loc": "/_ipx/_/logo.svg", + }, + ], + } + `) }) it('extracts videos from HTML', async () => {