astro-sitemap
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -290,3 +290,2 @@ // src/index.ts | ||
import { promises as fs2 } from "fs"; | ||
import { parse, HTMLElement } from "node-html-parser"; | ||
var addTailSlash = (s) => s.endsWith("/") ? s : s + "/"; | ||
@@ -301,23 +300,21 @@ var removeHeadingSlash = (s) => s.replace(/^\/+/, ""); | ||
var getFileFile = (pathname) => pathname ? `${removeTrailingSlash(pathname)}.html` : "index.html"; | ||
async function processPages(pages, dir, headHTML, buildFormat) { | ||
if (pages.length === 0) { | ||
return; | ||
} | ||
async function processPages(pages, dir, headHTML, buildFormat, logger2) { | ||
if (buildFormat !== "directory" && buildFormat !== "file") { | ||
throw new Error(`Unsupported build.format: '${buildFormat}' in your astro.config`); | ||
} | ||
let insertedCount = 0; | ||
const HEAD_END_TAG = "</head>"; | ||
for (const page of pages) { | ||
const fileUrl = new URL(buildFormat === "directory" ? getFileDir(page.pathname) : getFileFile(page.pathname), dir); | ||
const html = await fs2.readFile(fileUrl, "utf-8"); | ||
const root = parse(html); | ||
let head = root.querySelector("head"); | ||
if (!head) { | ||
head = new HTMLElement("head", {}, "", root); | ||
root.appendChild(head); | ||
console.warn(`No <head> found in \`${fileUrl.pathname}\`. <head> will be created.`); | ||
const content = await fs2.readFile(fileUrl, "utf-8"); | ||
const index = content.indexOf(HEAD_END_TAG); | ||
if (index === -1) { | ||
logger2.info(`Cannot insert links. Reason: no <head> section in \`${fileUrl.pathname}\`.`); | ||
} else { | ||
const inlined = content.substring(0, index) + headHTML + content.substring(index); | ||
await fs2.writeFile(fileUrl, inlined, "utf-8"); | ||
insertedCount += 1; | ||
} | ||
head.innerHTML = head.innerHTML + headHTML; | ||
const inlined = root.toString(); | ||
await fs2.writeFile(fileUrl, inlined, "utf-8"); | ||
} | ||
return insertedCount; | ||
} | ||
@@ -436,7 +433,9 @@ | ||
logger.success(`${fileNames.map((name) => `\`${name}\``).join(", ")} are created.`, `Total entries: ${urlData.length}.`); | ||
if (createLinkInHead) { | ||
if (createLinkInHead && srcPages.length > 0) { | ||
const sitemapHref = path.posix.join(config.base, fileNames[0]); | ||
const headHTML = `<link rel="sitemap" type="application/xml" href="${sitemapHref}">`; | ||
await processPages(srcPages, dir, headHTML, config.build.format); | ||
logger.success("Sitemap links are inserted into <head> section of generated pages."); | ||
const insertedCount = await processPages(srcPages, dir, headHTML, config.build.format, logger); | ||
if (insertedCount > 0) { | ||
logger.success(`Sitemap links are inserted into <head> section of generated pages (${insertedCount} of ${srcPages.length}).`); | ||
} | ||
} | ||
@@ -443,0 +442,0 @@ } catch (err) { |
{ | ||
"name": "astro-sitemap", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Generate a sitemap for Astro with more control", | ||
@@ -37,15 +37,14 @@ "keywords": [ | ||
"minimatch": "^5.1.0", | ||
"node-html-parser": "^5.3.3", | ||
"sitemap": "^7.1.1", | ||
"zod": "^3.17.10" | ||
"zod": "^3.18.0" | ||
}, | ||
"devDependencies": { | ||
"@types/minimatch": "^3.0.5", | ||
"@types/node": "^18.6.1", | ||
"astro": "^1.0.0-rc.1", | ||
"@types/node": "^18.7.11", | ||
"astro": "^1.0.7", | ||
"at-scripts": "0.0.4", | ||
"c8": "^7.12.0", | ||
"typescript": "^4.7.4", | ||
"vite": "^3.0.3", | ||
"vitest": "^0.19.1" | ||
"vite": "^3.0.9", | ||
"vitest": "^0.22.1" | ||
}, | ||
@@ -52,0 +51,0 @@ "publishConfig": { |
43684
4
469
- Removednode-html-parser@^5.3.3
- Removedboolbase@1.0.0(transitive)
- Removedcss-select@4.3.0(transitive)
- Removedcss-what@6.1.0(transitive)
- Removeddom-serializer@1.4.1(transitive)
- Removeddomelementtype@2.3.0(transitive)
- Removeddomhandler@4.3.1(transitive)
- Removeddomutils@2.8.0(transitive)
- Removedentities@2.2.0(transitive)
- Removedhe@1.2.0(transitive)
- Removednode-html-parser@5.4.2(transitive)
- Removednth-check@2.1.1(transitive)
Updatedzod@^3.18.0