-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prerenderer): extract lastmod from
article:modified_time
- Loading branch information
Showing
4 changed files
with
95 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { withSiteUrl } from 'nuxt-site-config-kit' | ||
import { parseURL } from 'ufo' | ||
import type { ResolvedSitemapUrl, SitemapUrl } from '../runtime/types' | ||
|
||
export function extractSitemapMetaFromHtml(html: string, options?: { images?: boolean; lastmod?: boolean; alternatives?: boolean }) { | ||
options = options || { images: true, lastmod: true, alternatives: true } | ||
const payload: Partial<SitemapUrl> = {} | ||
if (options?.images) { | ||
const images = new Set<string>() | ||
const mainRegex = /<main[^>]*>([\s\S]*?)<\/main>/ | ||
const mainMatch = mainRegex.exec(html) | ||
if (mainMatch?.[1] && mainMatch[1].includes('<img')) { | ||
// extract image src using regex on the html | ||
const imgRegex = /<img[^>]+src="([^">]+)"/g | ||
let match | ||
// eslint-disable-next-line no-cond-assign | ||
while ((match = imgRegex.exec(mainMatch[1])) !== null) { | ||
// This is necessary to avoid infinite loops with zero-width matches | ||
if (match.index === imgRegex.lastIndex) | ||
imgRegex.lastIndex++ | ||
let url = match[1] | ||
// if the match is relative | ||
if (url.startsWith('/')) | ||
url = withSiteUrl(url) | ||
images.add(url) | ||
} | ||
} | ||
if (images.size > 0) | ||
payload.images = [...images].map(i => ({ loc: i })) | ||
} | ||
|
||
if (options?.lastmod) { | ||
// let's extract the lastmod from the html using the following tags: | ||
const articleModifiedTime = html.match(/<meta[^>]+property="article:modified_time"[^>]+content="([^"]+)"/)?.[1] | ||
|| html.match(/<meta[^>]+content="([^"]+)"[^>]+property="article:modified_time"/)?.[1] | ||
if (articleModifiedTime) | ||
payload.lastmod = articleModifiedTime | ||
} | ||
|
||
if (options?.alternatives) { | ||
// do a loose regex match, get all alternative link lines | ||
// this is not tested | ||
const alternatives = (html.match(/<link[^>]+rel="alternate"[^>]+>/g) || []) | ||
.map((a) => { | ||
// extract the href, lang and type from the link | ||
const href = a.match(/href="([^"]+)"/)?.[1] | ||
const hreflang = a.match(/hreflang="([^"]+)"/)?.[1] | ||
return { hreflang, href: parseURL(href).pathname } | ||
}) | ||
.filter(a => a.hreflang && a.href) as ResolvedSitemapUrl['alternatives'] | ||
if (alternatives?.length && (alternatives.length > 1 || alternatives?.[0].hreflang !== 'x-default')) | ||
payload.alternatives = alternatives | ||
} | ||
return payload | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { extractSitemapMetaFromHtml } from '../../src/util/extractSitemapMetaFromHtml' | ||
|
||
describe('extractSitemapMetaFromHtml', () => { | ||
it('lastmod', async () => { | ||
// test article meta | ||
const output = extractSitemapMetaFromHtml(` | ||
<head> | ||
<meta property="article:published_time" content="2021-04-01T00:00:00Z"> | ||
<meta property="article:modified_time" content="2021-04-02T00:00:00Z"> | ||
</head> | ||
`) | ||
expect(output).toMatchInlineSnapshot(` | ||
{ | ||
"lastmod": "2021-04-02T00:00:00Z", | ||
} | ||
`) | ||
// test article meta | ||
const output2 = extractSitemapMetaFromHtml(` | ||
<head> | ||
<meta content="2021-04-01T00:00:00Z" property="article:published_time"/> | ||
<meta content="2021-04-02T00:00:00Z" property="article:modified_time"/> | ||
</head> | ||
`) | ||
expect(output2).toMatchInlineSnapshot(` | ||
{ | ||
"lastmod": "2021-04-02T00:00:00Z", | ||
} | ||
`) | ||
}) | ||
}) |