UNPKG

tiddlywiki-plugin-dev

Version:

[![](https://img.shields.io/badge/Join-TiddlyWiki_CN-blue)](https://github.com/tiddly-gittly)

152 lines (151 loc) 4.9 kB
import fs from "fs"; import path from "path"; import { tmpdir } from "os"; import { buildLibrary } from "./build.js"; import { rebuild } from "./packup.js"; import { tiddlywiki, mkdirsForFileSync, waitForFile } from "./utils.js"; const bypassTiddlers = /* @__PURE__ */ new Set([ "$:/core", "$:/UpgradeLibrary", "$:/UpgradeLibrary/List" ]); const headerMetadataTiddler = { title: "$:/Modern.TiddlyDev/no-cache-html", tags: ["$:/tags/RawMarkupWikified/TopHead"], text: [ "`", '<meta http-equiv="cache-control" content="no-cache">', '<meta http-equiv="expires" content="0">', '<meta http-equiv="pragma" content="no-cache">', "`" ].join("\n") }; const publishOnlineHTML = async (wikiPath = "wiki", dist = "dist", htmlName = "index.html", excludeFilter = "-[is[draft]]", library = true, srcPath = "src", excludePlugin) => { const wikiFolder = path.resolve(wikiPath); const distDir = path.resolve(dist); const $tw = tiddlywiki([], wikiFolder); const tiddlers = {}; const savePromises = []; mkdirsForFileSync(path.resolve(distDir, "media", "1")); $tw.wiki.each(({ fields }, title) => { if (bypassTiddlers.has(title) || title.startsWith("$:/boot/") || title.startsWith("$:/temp/")) { return; } if ($tw.wiki.isBinaryTiddler(title) || $tw.wiki.isImageTiddler(title)) { const { extension, encoding } = $tw.config.contentTypeInfo[fields.type || "text/vnd.tiddlywiki"] ?? { extension: ".bin", encoding: "base64" }; const fileName = encodeURIComponent( title.endsWith(extension) ? title : `${title}${extension}` ); savePromises.push( new Promise( (resolve) => fs.writeFile( path.resolve(distDir, "media", fileName), fields.text, encoding, resolve ) ) ); tiddlers[title] = { ...fields, text: "", _canonical_uri: `./media/${encodeURIComponent(fileName)}` }; } else { tiddlers[title] = { ...fields }; } }); Object.entries( library ? await buildLibrary( path.join(dist, "library"), excludePlugin, srcPath, wikiPath ) : await rebuild(tiddlywiki(), srcPath, void 0, false, excludeFilter) ).forEach(([title, tiddler]) => tiddlers[title] = tiddler); tiddlers[headerMetadataTiddler.title] = headerMetadataTiddler; const tmpFolder = fs.mkdtempSync(path.resolve(tmpdir(), "tiddlywiki-")); try { fs.cpSync( path.resolve(wikiFolder, "tiddlywiki.info"), path.resolve(tmpFolder, "tiddlywiki.info") ); tiddlywiki(Object.values(tiddlers), tmpFolder, [ ...["--output", distDir], ...[ "--rendertiddler", "$:/core/save/offline-external-js", htmlName, "text/plain", "", "publishFilter", excludeFilter ], ...[ "--rendertiddler", "$:/core/templates/tiddlywiki5.js", `tiddlywikicore-${$tw.version}.js`, "text/plain" ] ]); await waitForFile( path.resolve(distDir, `tiddlywikicore-${$tw.version}.js`) ); } catch (e) { console.error(e); } fs.rmSync(tmpFolder, { recursive: true, force: true }); (await Promise.all(savePromises)).forEach((error) => { if (error) { console.error(error); } }); }; const publishOfflineHTML = async (wikiPath = "wiki", dist = "dist", htmlName = "index.html", excludeFilter = "-[is[draft]]", library = true, srcPath = "src", excludePlugin) => { const distDir = path.resolve(dist); const wikiFolder = path.resolve(wikiPath); const $tw = tiddlywiki([], wikiFolder); const tiddlers = {}; $tw.wiki.each(({ fields }, title) => { if (bypassTiddlers.has(title) || title.startsWith("$:/boot/") || title.startsWith("$:/temp/")) { return; } tiddlers[title] = { ...fields }; }); Object.entries( library ? await buildLibrary( path.join(dist, "library"), excludePlugin, srcPath, wikiPath ) : await rebuild(tiddlywiki(), srcPath, void 0, false, excludeFilter) ).forEach(([title, tiddler]) => tiddlers[title] = tiddler); tiddlers[headerMetadataTiddler.title] = headerMetadataTiddler; const tmpFolder = fs.mkdtempSync(path.resolve(tmpdir(), "tiddlywiki-")); try { fs.cpSync( path.resolve(wikiFolder, "tiddlywiki.info"), path.resolve(tmpFolder, "tiddlywiki.info") ); tiddlywiki(Object.values(tiddlers), tmpFolder, [ ...["--output", distDir], ...[ "--rendertiddler", "$:/plugins/tiddlywiki/tiddlyweb/save/offline", htmlName, "text/plain", "", "publishFilter", excludeFilter ] ]); await waitForFile(path.join(distDir, htmlName)); } catch (e) { console.error(e); } fs.rmSync(tmpFolder, { recursive: true, force: true }); }; export { publishOfflineHTML, publishOnlineHTML };