Skip to content

Commit

Permalink
no error with no directory
Browse files Browse the repository at this point in the history
  • Loading branch information
KishiTheMechanic committed Oct 27, 2024
1 parent c68628e commit 1bc6908
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { globToRegExp, join, normalize, SEPARATOR } from 'jsr:@std/[email protected]'
import { ensureFile } from 'jsr:@std/[email protected]'
import { ensureFile, exists } from 'jsr:@std/[email protected]'

export interface SiteMapEntry {
loc: string // URL location included in the <loc> tag
Expand Down Expand Up @@ -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
Expand All @@ -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$/,
Expand All @@ -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]

Expand Down

0 comments on commit 1bc6908

Please sign in to comment.