From 1bc69084e95a7dc8d1438b8e4ff12771a56ac5ab Mon Sep 17 00:00:00 2001 From: KishiTheMechanic Date: Mon, 28 Oct 2024 00:13:07 +0100 Subject: [PATCH] no error with no directory --- mod.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/mod.ts b/mod.ts index 63ccd69..f55349c 100644 --- a/mod.ts +++ b/mod.ts @@ -1,5 +1,5 @@ import { globToRegExp, join, normalize, SEPARATOR } from 'jsr:@std/path@0.224.0' -import { ensureFile } from 'jsr:@std/fs@0.224.0' +import { ensureFile, exists } from 'jsr:@std/fs@0.224.0' export interface SiteMapEntry { loc: string // URL location included in the tag @@ -119,6 +119,7 @@ async function generateSitemap( /** * Generates sitemap entries for markdown articles, respecting language settings. + * Checks if the articles directory exists before processing. * @param basename - The base URL * @param articlesDirectory - Directory containing article markdown files * @param options - Options for sitemap generation, including languages @@ -132,6 +133,11 @@ async function generateArticlesSitemap( const sitemap: Sitemap = [] const languages = options.languages || [] + // Check if articlesDirectory exists; if not, return an empty sitemap + if (!(await exists(articlesDirectory))) { + return sitemap + } + async function addMarkdownFile(path: string) { const relPath = path.substring(articlesDirectory.length).replace( /\.md$/, @@ -143,11 +149,8 @@ async function generateArticlesSitemap( const pathname = normalize(`/${segments.join('/')}`).replace(/\/index$/, '') const urlPaths = languages.length > 0 - ? languages.map(( - lang, - ) => (lang === options.defaultLanguage - ? pathname - : `/${lang}${pathname}`) + ? languages.map((lang) => + lang === options.defaultLanguage ? pathname : `/${lang}${pathname}` ) : [pathname]