1- import { open } from "node:fs/promises" ;
2- import type { XMLDocument } from "libxmljs" ;
3- import { parseXmlAsync } from "libxmljs" ;
1+ import fs from "node:fs" ;
2+ import { XmlDocument } from "libxml2-wasm" ;
43
54async function loadSitemap ( ) {
6- const file = await open ( "./public/sitemap.xml" ) ;
7-
8- const document = parseXmlAsync ( await file . readFile ( ) ) ;
5+ const document = XmlDocument . fromString (
6+ fs . readFileSync ( "./public/sitemap.xml" ) . toString ( )
7+ ) ;
98
109 return document ;
1110}
1211
13- function extractUrlsFromSitemap ( sitemap : XMLDocument ) {
12+ function extractUrlsFromSitemap ( sitemap : XmlDocument ) {
1413 const urlElements = sitemap . find ( "*" ) ;
1514 const urls = urlElements . map ( ( ele ) => {
16- // const name = ele.find("*")[0].childNodes()[0].text();
1715 const children = ele . find ( "*" ) ;
18- const name = children [ 0 ] . childNodes ( ) [ 0 ] . text ( ) ;
16+ const name = children [ 0 ] . find ( "*" ) [ 0 ] . content ;
1917 let lastmod = null ;
2018 if ( children . length > 1 ) {
21- lastmod = children [ 1 ] . childNodes ( ) [ 0 ] . text ( ) ;
19+ lastmod = children [ 1 ] . find ( "*" ) [ 0 ] . content ;
2220 }
2321 return { name, lastmod } ;
2422 } ) ;
@@ -29,16 +27,20 @@ function extractUrlsFromSitemap(sitemap: XMLDocument) {
2927describe ( "Sitemap" , ( ) => {
3028 it ( "should have a version and encoding" , async ( ) => {
3129 const subject = await loadSitemap ( ) ;
32- expect ( subject . version ( ) ) . toEqual ( "1.0" ) ;
33- expect ( subject . encoding ( ) ) . toEqual ( "UTF-8" ) ;
30+ expect ( subject . get ( "version" ) ) . toEqual ( "1.0" ) ;
31+ expect ( subject . get ( "encoding" ) ) . toEqual ( "UTF-8" ) ;
32+
33+ subject . dispose ( ) ;
3434 } ) ;
3535
3636 it ( "should have a schema listed" , async ( ) => {
3737 const subject = await loadSitemap ( ) ;
38- const urlsetNode = subject . root ( ) ! ;
39- expect ( urlsetNode ! . namespace ( ) ! . href ( ) ) . toEqual (
40- "http://www.sitemaps.org/schemas/sitemap/0.9" ,
38+ const urlsetNode = subject . root ! ;
39+ expect ( urlsetNode ! . namespaceUri ) . toEqual (
40+ "http://www.sitemaps.org/schemas/sitemap/0.9"
4141 ) ;
42+
43+ subject . dispose ( ) ;
4244 } ) ;
4345
4446 it ( "should have pages for the index" , async ( ) => {
@@ -48,6 +50,8 @@ describe("Sitemap", () => {
4850 const urlNames = urls . map ( ( url ) => url . name ) ;
4951 // index page
5052 expect ( urlNames ) . toContain ( "https://hockeybuggy.com" ) ;
53+
54+ subject . dispose ( ) ;
5155 } ) ;
5256
5357 it ( "should have pages for the blog" , async ( ) => {
@@ -64,7 +68,7 @@ describe("Sitemap", () => {
6468 expect ( blogPosts . length ) . toBeGreaterThan ( 10 ) ;
6569 // All the blog posts have a lastmod date
6670 expect (
67- blogPosts . map ( ( post ) => post . lastmod ) . every ( ( post ) => post !== null ) ,
71+ blogPosts . map ( ( post ) => post . lastmod ) . every ( ( post ) => post !== null )
6872 ) . toBe ( true ) ;
6973
7074 // tags pages
@@ -78,10 +82,12 @@ describe("Sitemap", () => {
7882 expect ( urlNames ) . toContain ( "https://hockeybuggy.com/blog/categories" ) ;
7983 const blogCategoriesPattern = / \/ b l o g \/ c a t e g o r i e s / ;
8084 const blogCategoriesPages = urls . filter ( ( url ) =>
81- blogCategoriesPattern . test ( url . name ) ,
85+ blogCategoriesPattern . test ( url . name )
8286 ) ;
8387 // There are category pages. 4 is an arbitary number.
8488 expect ( blogCategoriesPages . length ) . toBeGreaterThan ( 4 ) ;
89+
90+ subject . dispose ( ) ;
8591 } ) ;
8692
8793 it ( "should have pages for the project" , async ( ) => {
@@ -94,9 +100,10 @@ describe("Sitemap", () => {
94100
95101 const projectPagePattern = / \/ p r o j e c t \/ \w * / ;
96102 const projectPages = urls . filter ( ( url ) =>
97- projectPagePattern . test ( url . name ) ,
103+ projectPagePattern . test ( url . name )
98104 ) ;
99105 // There are post pages. 5 is an arbitary number.
100106 expect ( projectPages . length ) . toBeGreaterThan ( 5 ) ;
107+ subject . dispose ( ) ;
101108 } ) ;
102109} ) ;
0 commit comments