prettifies
This commit is contained in:
+22
-14
@@ -1,7 +1,7 @@
|
|||||||
import type {AstroIntegration, ContentEntryType, HookParameters} from "astro"
|
import type {AstroIntegration, ContentEntryType, HookParameters} from "astro"
|
||||||
import {fileURLToPath} from "url"
|
import {fileURLToPath} from "url"
|
||||||
import gemtextVitePlugin from "./vitePlugin.js"
|
import gemtextVitePlugin from "./vitePlugin.js"
|
||||||
import { parse as parseYaml } from "yaml";
|
import {parse as parseYaml} from "yaml"
|
||||||
|
|
||||||
/** Gemtext configuration options */
|
/** Gemtext configuration options */
|
||||||
export interface GemtextConfig {
|
export interface GemtextConfig {
|
||||||
@@ -31,26 +31,34 @@ export default function gemtext(config: GemtextConfig = {}): AstroIntegration {
|
|||||||
hooks: {
|
hooks: {
|
||||||
"astro:config:setup": async (_params) => {
|
"astro:config:setup": async (_params) => {
|
||||||
const params = _params as SetupHookParams
|
const params = _params as SetupHookParams
|
||||||
params.addPageExtension('.gmi')
|
params.addPageExtension(".gmi")
|
||||||
params.addContentEntryType({
|
params.addContentEntryType({
|
||||||
extensions: ['.gmi'],
|
extensions: [".gmi"],
|
||||||
getEntryInfo: async ({fileUrl, contents}) => {
|
getEntryInfo: async ({fileUrl, contents}) => {
|
||||||
const lines = contents.split('\n');
|
const lines = contents.split("\n")
|
||||||
let data: Record<string, unknown> = {};
|
let data: Record<string, unknown> = {}
|
||||||
let frontmatter = '';
|
let frontmatter = ""
|
||||||
let bodyStartLineIndex = 0;
|
let bodyStartLineIndex = 0
|
||||||
|
|
||||||
if (lines.at(0) === '---' && lines.lastIndexOf('---') > 0) {
|
if (lines.at(0) === "---" && lines.lastIndexOf("---") > 0) {
|
||||||
bodyStartLineIndex = lines.indexOf('---', 1) + 1;
|
bodyStartLineIndex = lines.indexOf("---", 1) + 1
|
||||||
frontmatter = lines.slice(1, bodyStartLineIndex - 1).join('\n');
|
frontmatter = lines
|
||||||
|
.slice(1, bodyStartLineIndex - 1)
|
||||||
|
.join("\n")
|
||||||
try {
|
try {
|
||||||
data = { ...data, ...parseYaml(frontmatter, { schema: 'yaml-1.1' }) };
|
data = {
|
||||||
|
...data,
|
||||||
|
...parseYaml(frontmatter, {schema: "yaml-1.1"}),
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.debug('Failed to parse YAML frontmatter in gemtext:', 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 =
|
const slug =
|
||||||
typeof data["slug"] === "string" ?
|
typeof data["slug"] === "string" ?
|
||||||
@@ -64,7 +72,7 @@ export default function gemtext(config: GemtextConfig = {}): AstroIntegration {
|
|||||||
rawData: frontmatter,
|
rawData: frontmatter,
|
||||||
body,
|
body,
|
||||||
slug,
|
slug,
|
||||||
};
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
params.updateConfig({
|
params.updateConfig({
|
||||||
|
|||||||
+1
-1
@@ -54,7 +54,7 @@ export default function gemtextVitePlugin(config: GemtextConfig): Plugin {
|
|||||||
name: "astro-gemtext",
|
name: "astro-gemtext",
|
||||||
enforce: "pre",
|
enforce: "pre",
|
||||||
transform(code, id) {
|
transform(code, id) {
|
||||||
if (!id.endsWith('.gmi')) return
|
if (!id.endsWith(".gmi")) return
|
||||||
return {
|
return {
|
||||||
code: transform(code, id, config),
|
code: transform(code, id, config),
|
||||||
map: null,
|
map: null,
|
||||||
|
|||||||
Reference in New Issue
Block a user