diff options
| author | Joe Carstairs <me@joeac.net> | 2026-03-21 20:38:22 +0000 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2026-03-21 20:38:22 +0000 |
| commit | c3c710107de80feb2bc31a35d3e49220456882b3 (patch) | |
| tree | bf84e63c000dda3efd3a35d1f821daa65f2e6c80 /src/index.ts | |
| parent | 5663e18f8d9780053164ea1a97cd1a52eb6745f5 (diff) | |
prettifies
Diffstat (limited to 'src/index.ts')
| -rw-r--r-- | src/index.ts | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/src/index.ts b/src/index.ts index 83ffc75..47e82da 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import type {AstroIntegration, ContentEntryType, HookParameters} from "astro" import {fileURLToPath} from "url" import gemtextVitePlugin from "./vitePlugin.js" -import { parse as parseYaml } from "yaml"; +import {parse as parseYaml} from "yaml" /** Gemtext configuration options */ export interface GemtextConfig { @@ -31,26 +31,34 @@ export default function gemtext(config: GemtextConfig = {}): AstroIntegration { hooks: { "astro:config:setup": async (_params) => { const params = _params as SetupHookParams - params.addPageExtension('.gmi') + params.addPageExtension(".gmi") params.addContentEntryType({ - extensions: ['.gmi'], - getEntryInfo: async ({ fileUrl, contents }) => { - const lines = contents.split('\n'); - let data: Record<string, unknown> = {}; - let frontmatter = ''; - let bodyStartLineIndex = 0; + extensions: [".gmi"], + getEntryInfo: async ({fileUrl, contents}) => { + const lines = contents.split("\n") + let data: Record<string, unknown> = {} + let frontmatter = "" + let bodyStartLineIndex = 0 - if (lines.at(0) === '---' && lines.lastIndexOf('---') > 0) { - bodyStartLineIndex = lines.indexOf('---', 1) + 1; - frontmatter = lines.slice(1, bodyStartLineIndex - 1).join('\n'); - try { - data = { ...data, ...parseYaml(frontmatter, { schema: 'yaml-1.1' }) }; - } catch (e) { - console.debug('Failed to parse YAML frontmatter in gemtext:', e); + if (lines.at(0) === "---" && lines.lastIndexOf("---") > 0) { + bodyStartLineIndex = lines.indexOf("---", 1) + 1 + frontmatter = lines + .slice(1, bodyStartLineIndex - 1) + .join("\n") + try { + data = { + ...data, + ...parseYaml(frontmatter, {schema: "yaml-1.1"}), + } + } catch (e) { + console.debug( + "Failed to parse YAML frontmatter in gemtext:", + e, + ) + } } - } - const body = lines.slice(bodyStartLineIndex).join('\n'); + const body = lines.slice(bodyStartLineIndex).join("\n") const slug = typeof data["slug"] === "string" ? @@ -59,12 +67,12 @@ export default function gemtext(config: GemtextConfig = {}): AstroIntegration { fileURLToPath(fileUrl).lastIndexOf("/") + 1, ) - return { - data, - rawData: frontmatter, - body, - slug, - }; + return { + data, + rawData: frontmatter, + body, + slug, + } }, }) params.updateConfig({ |
