From f909e1adc934b26dd02009e8d075f5c7e201b2cc Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Tue, 24 Mar 2026 21:20:02 +0000 Subject: [PATCH] try to extract title, pubDate, updatedDate automagically --- src/index.ts | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 86ef18a..7a7eb96 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,12 +52,23 @@ export default function gemtext(config: GemtextConfig = {}): AstroIntegration { const body = lines.slice(bodyStartLineIndex).join("\n") - const slug = - typeof data["slug"] === "string" ? - data["slug"] - : fileURLToPath(fileUrl).slice( - fileURLToPath(fileUrl).lastIndexOf("/") + 1, - ) + const filepath = fileURLToPath(fileUrl); + const filename = filepath.slice(filepath.lastIndexOf("/") + 1); + const slug = + typeof data["slug"] === "string" ? + data["slug"] + : filename; + + data['title'] = data['title'] + ?? lines.slice(bodyStartLineIndex).find(line => line.startsWith('# '))?.slice(2); + data['pubDate'] = data['pubDate'] + ?? validDateOrUndefined(lines.slice(bodyStartLineIndex).find(line => line.startsWith('Published on: '))?.slice(14)) + ?? validDateOrUndefined(filename.replace(/\.[^.]\+$/, '')) + ?? validDateOrUndefined(slug.replace(/\.[^.]\+$/, '')); + data['updatedDate'] = data['updatedDate'] + ?? validDateOrUndefined(lines.slice(bodyStartLineIndex).find(line => line.startsWith('Updated on: '))?.slice(14)) + ?? validDateOrUndefined(filename.replace(/\.[^.]\+$/, '')) + ?? validDateOrUndefined(slug.replace(/\.[^.]\+$/, '')); return { data, @@ -71,3 +82,11 @@ export default function gemtext(config: GemtextConfig = {}): AstroIntegration { }, } } + +const validDateOrUndefined = (x: unknown) => { + const d = new Date(x as string); + if (isNaN(d.valueOf())) { + return undefined; + } + return d; +};