Skip to content

Commit

Permalink
feat: config discoverImages to disable image discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Apr 1, 2023
1 parent c68cc44 commit 9d50519
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -448,6 +448,13 @@ Will generate routes from your static page files. Useful to disable if you're us

The path to the XSL stylesheet for the sitemap.xml. Set to `false` to disable.

### `discoverImages`

- Type: `boolean`
- Default: `true`

Whether to discover images from routes when prerendering.

## Sponsors

<p align="center">
Expand Down
29 changes: 19 additions & 10 deletions src/module.ts
Expand Up @@ -43,6 +43,12 @@ export interface ModuleOptions extends CreateFilterOptions, SitemapRoot {
* @default /__sitemap__/style.xsl
*/
xsl: string | false
/**
* When prerendering, should images be automatically be discovered and added to the sitemap.
*
* @default true
*/
discoverImages: boolean
}

export interface ModuleHooks {
Expand Down Expand Up @@ -70,6 +76,7 @@ export default defineNuxtModule<ModuleOptions>({
siteUrl: process.env.NUXT_PUBLIC_SITE_URL || nuxt.options.runtimeConfig.public?.siteUrl,
trailingSlash: String(trailingSlash) === 'true',
inferStaticPagesAsRoutes: true,
discoverImages: true,
// index sitemap options filtering
include: ['/**'],
exclude: [],
Expand Down Expand Up @@ -215,16 +222,18 @@ export {}
const mainMatch = mainRegex.exec(html)
if (!mainMatch)
return
// 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) {
const url = new URL(match[1], config.siteUrl)
sitemapImages[ctx.route] = sitemapImages[ctx.route] || []
sitemapImages[ctx.route].push({
loc: url.href,
})
if (config.discoverImages) {
// 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) {
const url = new URL(match[1], config.siteUrl)
sitemapImages[ctx.route] = sitemapImages[ctx.route] || []
sitemapImages[ctx.route].push({
loc: url.href,
})
}
}
}
})
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/routes/document-driven-urls.ts
@@ -1,6 +1,6 @@
import { defineEventHandler } from 'h3'
import type { ResolvedSitemapEntry } from '../../types'
import { useStorage } from '#imports'
import { useStorage, useRuntimeConfig } from '#imports'

export default defineEventHandler(async () => {
const prefix = 'cache:content:parsed:content'
Expand All @@ -13,11 +13,11 @@ export default defineEventHandler(async () => {
const meta = await useStorage().getMeta(k.replace(prefix, 'content:source:content'))
const item = await useStorage().getItem(k)
// add any top level images
const images = item?.parsed.body?.children
const images = useRuntimeConfig()['nuxt-simple-sitemap'].discoverImages ? (item?.parsed.body?.children
?.filter(c => ['image', 'img', 'nuxtimg', 'nuxt-img'].includes(c.tag?.toLowerCase()) && c.props?.src)
.map(i => ({
loc: i.props.src,
})) || []
})) || []) || [];
const loc = k.replace(prefix, '')
.replaceAll(':', '/')
// need to strip out the leading number such as 0.index.md -> index.md
Expand Down
4 changes: 2 additions & 2 deletions test/generate.test.ts
Expand Up @@ -54,7 +54,7 @@ describe('generate', () => {
<image:loc>https://example.com/image2.jpg</image:loc>
</image:image>
<image:image>
<image:url>https://res.cloudinary.com/dl6o1xpyq/image/upload/f_jpg,q_auto:best,dpr_auto,w_240,h_240/images/harlan-wilton</image:url>
<image:loc>https://res.cloudinary.com/dl6o1xpyq/image/upload/f_jpg,q_auto:best,dpr_auto,w_240,h_240/images/harlan-wilton</image:loc>
</image:image>
<lastmod>2023-02-21T08:50:52+00:00</lastmod>
<changefreq>daily</changefreq>
Expand All @@ -63,7 +63,7 @@ describe('generate', () => {
<url>
<loc>https://nuxt-simple-sitemap.com/new-page</loc>
<image:image>
<image:url>https://res.cloudinary.com/dl6o1xpyq/image/upload/f_jpg,q_auto:best,dpr_auto,w_240,h_240/images/harlan-wilton</image:url>
<image:loc>https://res.cloudinary.com/dl6o1xpyq/image/upload/f_jpg,q_auto:best,dpr_auto,w_240,h_240/images/harlan-wilton</image:loc>
</image:image>
</url>
<url>
Expand Down

0 comments on commit 9d50519

Please sign in to comment.