From 402938b897571932703e9d22d8ea7a73391dda81 Mon Sep 17 00:00:00 2001 From: ache Date: Wed, 17 Jul 2024 00:03:47 +0200 Subject: Update toml parser --- package.json | 5 +++-- src/build/loadMD.mjs | 57 +++++++++++++++++++++++++++++----------------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 3cc65c2..72bf3be 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,6 @@ "author": "ache", "license": "MIT", "devDependencies": { - "@ltd/j-toml": "^1.30.0", "cssesc": "^3.0.0", "hast-util-raw": "^7.2.1", "hast-util-select": "^5.0.1", @@ -60,8 +59,10 @@ "rss": "^1.2.2", "sitemap": "^7.1.1", "terser": "^5.12.1", - "toml": "^3.0.0", "unist-builder": "^3.0.0", "unist-util-inspect": "^7.0.0" + }, + "dependencies": { + "smol-toml": "^1.2.2" } } diff --git a/src/build/loadMD.mjs b/src/build/loadMD.mjs index 6ba4f76..a1eab3b 100644 --- a/src/build/loadMD.mjs +++ b/src/build/loadMD.mjs @@ -1,20 +1,20 @@ -import fs from 'node:fs'; -import {u} from 'unist-builder'; -import {h} from 'hastscript'; -import {select} from 'hast-util-select'; -import {toString as hastToString} from 'mdast-util-to-string'; -import cssesc from 'cssesc'; -import toml from '@ltd/j-toml'; +import fs from "node:fs"; +import { u } from "unist-builder"; +import { h } from "hastscript"; +import { select } from "hast-util-select"; +import { toString as hastToString } from "mdast-util-to-string"; +import cssesc from "cssesc"; +import toml from "smol-toml"; -import {toString, toMdRaw, mdToHtmlRaw} from './to-html.mjs'; -import {getArticleYear} from './article.mjs'; -import i18n from './i18n.mjs'; +import { toString, toMdRaw, mdToHtmlRaw } from "./to-html.mjs"; +import { getArticleYear } from "./article.mjs"; +import i18n from "./i18n.mjs"; const loadMD = (listFile, suffix, lang) => { const listContent = []; for (const file of listFile) { console.log(`Working on ${file}`); - const content = fs.readFileSync(`${suffix}/${file}`, 'utf8'); + const content = fs.readFileSync(`${suffix}/${file}`, "utf8"); const mdRaw = toMdRaw(content); const tomlStringValue = mdRaw.children[0].value; const metaData = toml.parse(tomlStringValue); @@ -23,20 +23,20 @@ const loadMD = (listFile, suffix, lang) => { const htmlContent = newHTML; const htmlRender = toString(htmlContent); - const titleHtml = select('h1', htmlContent); - const intro = select('p', htmlContent); - intro.children = intro.children.filter(child => child.tagName !== 'br'); + const titleHtml = select("h1", htmlContent); + const intro = select("p", htmlContent); + intro.children = intro.children.filter((child) => child.tagName !== "br"); - const logo = select('img', intro); + const logo = select("img", intro); if (logo && logo?.properties) { - if (logo.properties.src[0] != '/') { + if (logo.properties.src[0] != "/") { logo.properties.src = `/${suffix}/${logo.properties.src}`; } - logo.properties.height = '150'; - logo.properties.width = '150'; + logo.properties.height = "150"; + logo.properties.width = "150"; } - const logoP = select('source', intro); + const logoP = select("source", intro); if (logoP !== null) { logoP.properties.srcSet = `/${suffix}/${logoP.properties.srcSet}`; } @@ -44,27 +44,34 @@ const loadMD = (listFile, suffix, lang) => { titleHtml.children[0].properties.href = `/${suffix}/${file.slice(0, -3)}`; const title = hastToString(titleHtml); - const domTitle = cssesc(title.replace(/\s+/g, '-').replace(/['"#@]/, '').toLowerCase()); // Maybe encodeURI + const domTitle = cssesc( + title + .replace(/\s+/g, "-") + .replace(/['"#@]/, "") + .toLowerCase(), + ); // Maybe encodeURI - const readMore = h('a', i18n[lang]['read_more']); + const readMore = h("a", i18n[lang]["read_more"]); readMore.properties.href = `/${suffix}/${file.slice(0, -3)}`; - const pubYear = getArticleYear({metaData}); + const pubYear = getArticleYear({ metaData }); if (metaData.pubDate) { try { metaData.pubDateISO = metaData.pubDate.toISOString(); } catch (error) { - console.error(`Error on file ${file} with pubDate (${metaData.pubDate}): ${error}`); + console.error( + `Error on file ${file} with pubDate (${metaData.pubDate}): ${error}`, + ); } } listContent.push({ name: file.slice(0, -3), content: htmlRender, - intro: toString(u('root', [titleHtml, intro, readMore])), + intro: toString(u("root", [titleHtml, intro, readMore])), introDesc: hastToString(intro), - imageUrl: logo?.properties?.src || '', + imageUrl: logo?.properties?.src || "", metaData, pubYear, title, -- cgit v1.2.3-70-g09d2