Skip to content

Commit

Permalink
add externaltext
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacharia2 committed Apr 10, 2024
1 parent 8c8ebbb commit ac6a0d4
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 92 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ tiddlywiki deploy GitHub pages
>
> (已启用此功能)缓存策略 public\service-worker.js 配置中 index.html 的 StaleWhileRevalidate 策略。
## 其它

Shx 是一个包装 ShellJS Unix 命令的包装器,为 npm 包脚本中简单的类 Unix 跨平台命令提供了一个简单的解决方案

Google的zx库 可以帮助我们更加高效地用Nodejs写shell 脚本。
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
{
"scripts": {
"zx-build": "zx ./scripts/build-wiki.mjs",
"server": "npx cross-env TIDDLYWIKI_PLUGIN_PATH='./plugins' TIDDLYWIKI_THEME_PATH='./themes' TIDDLYWIKI_LANGUAGE_PATH='./languages' npx tiddlywiki +plugins/tiddlywiki/filesystem +plugins/tiddlywiki/tiddlyweb . --listen port=8080",
"buildonline": "node -e \"require('./scripts/build').buildOnlineHTML('public-dist', 'index.html');\"",
"buildoffline": "node -e \"require('./scripts/build').buildOfflineHTML('public-dist', 'offline.html');\"",
"buildlibrary": "node -e \"require('./scripts/build').buildLibrary('[prefix[$:/]!prefix[$:/plugins/tiddlywiki/]!prefix[$:/themes/tiddlywiki/]!prefix[$:/languages/]!tag[$:/tags/PluginLibrary]!prefix[$:/plugins/kookma]]', 'public-dist/library');\"",
"buildonline": "zx ./scripts/build-online.mjs",
"buildoffline": "zx ./scripts/build-offline.mjs",
"buildlibrary": "zx ./scripts/build-library.mjs",
"clean": "rimraf public-dist"
},
"dependencies": {
"cross-env": "^7.0.3",
"html-minifier-terser": "^7.2.0",
"rimraf": "^5.0.5",
"shx": "0.3.4",
"tiddlywiki": "^5.3.3",
"uglify-js": "^3.17.4",
"workbox-cli": "7.0.0",
"zx": "^8.0.1",
"cross-spawn": "^7.0.3"
"zx": "^8.0.1"
}
}
61 changes: 12 additions & 49 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions scripts/build-library.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { fs, $ } from 'zx';
import path from 'path';

/** 项目路径 */
const repoFolder = path.join(path.dirname(__filename), '..');
const distDir = path.join(repoFolder, 'public-dist/library');


/** 设置环境变量,TW会同时在自己的源码路径以及环境变量定义的路径中寻找插件、主题和语言
* 如果不这样写,plugins、themes、languages和editions里的内容就无法被加载
*/
process.env.TIDDLYWIKI_PLUGIN_PATH = `${repoFolder}/plugins`;
process.env.TIDDLYWIKI_THEME_PATH = `${repoFolder}/themes`;
process.env.TIDDLYWIKI_LANGUAGE_PATH = `${repoFolder}/languages`;
process.env.TIDDLYWIKI_EDITION_PATH = `${repoFolder}/editions`;


const minify = true;
// const pluginFilter = '[prefix[$:/plugins/]!prefix[$:/plugins/tiddlywiki/]!prefix[$:/languages/]!prefix[$:/themes/tiddlywiki/]!tag[$:/tags/PluginLibrary]]';
const pluginFilter = '[prefix[$:/]!prefix[$:/plugins/tiddlywiki/]!prefix[$:/themes/tiddlywiki/]!prefix[$:/languages/]!tag[$:/tags/PluginLibrary]!prefix[$:/plugins/kookma]]';

await $`npx tiddlywiki . \\
--output ${distDir} \\
--makelibrary $:/UpgradeLibrary \\
--savelibrarytiddlers $:/UpgradeLibrary ${pluginFilter} recipes/library/tiddlers/ $:/UpgradeLibrary/List \\
--savetiddler $:/UpgradeLibrary/List recipes/library/tiddlers.json \\
--rendertiddler $:/plugins/tiddlywiki/pluginlibrary/library.template.html index-raw.html text/plain \\
--deletetiddlers \'[[$:/UpgradeLibrary]] [[$:/UpgradeLibrary/List]]\' \\
`, { env: { TIDDLYWIKI_PLUGIN_PATH: path.resolve(distDir, '..', 'plugins') } };

// 最小化:HTML
if (minify) {
await $`npx html-minifier-terser -c scripts/html-minifier-terser.config.json -o ${distDir}/index.html ${distDir}/index-raw.html && rm ${distDir}/index-raw.html`;
} else {
await $`mv ${distDir}/index-raw.html ${distDir}/${htmlName}`;
}
30 changes: 30 additions & 0 deletions scripts/build-offline.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { fs, $ } from 'zx';
import path from 'path';

/** 项目路径 */
const repoFolder = path.join(path.dirname(__filename), '..');
const distDir = path.join(repoFolder, 'public-dist');

/** 设置环境变量,TW会同时在自己的源码路径以及环境变量定义的路径中寻找插件、主题和语言
* 如果不这样写,plugins、themes、languages和editions里的内容就无法被加载
*/
process.env.TIDDLYWIKI_PLUGIN_PATH = `${repoFolder}/plugins`;
process.env.TIDDLYWIKI_THEME_PATH = `${repoFolder}/themes`;
process.env.TIDDLYWIKI_LANGUAGE_PATH = `${repoFolder}/languages`;
process.env.TIDDLYWIKI_EDITION_PATH = `${repoFolder}/editions`;

const htmlName = 'offline.html';
const minify = true;
const excludeFilter = '-[is[draft]]';


// 构建HTML
await $`npx tiddlywiki . --output ${distDir} --build deletetiddlers \\
--rendertiddler $:/core/save/all index-raw.html text/plain "" publishFilter ${excludeFilter}`;

// 最小化:HTML
if (minify) {
await $`npx html-minifier-terser -c scripts/html-minifier-terser.config.json -o ${distDir}/${htmlName} ${distDir}/index-raw.html && rm ${distDir}/index-raw.html`;
} else {
await $`mv ${distDir}/index-raw.html ${distDir}/${htmlName}`;
}
59 changes: 59 additions & 0 deletions scripts/build-online.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { fs, $ } from 'zx';
import path from 'path';

/** 项目路径 */
const repoFolder = path.join(path.dirname(__filename), '..');
const distDir = path.join(repoFolder, 'public-dist');
async function getVersion() {
const versionOutput = await $`npx tiddlywiki . --version`;
const versionRegex = /^[0-9]+\.[0-9]+\.[0-9]+/;
const match = versionOutput.stdout.match(versionRegex);

if (match) {
const version = match[0];
return version
} else {
throw new Error('无法解析版本号');
}
}
const tw_Version = await getVersion()
/** 设置环境变量,TW会同时在自己的源码路径以及环境变量定义的路径中寻找插件、主题和语言
* 如果不这样写,plugins、themes、languages和editions里的内容就无法被加载
*/
process.env.TIDDLYWIKI_PLUGIN_PATH = `${repoFolder}/plugins`;
process.env.TIDDLYWIKI_THEME_PATH = `${repoFolder}/themes`;
process.env.TIDDLYWIKI_LANGUAGE_PATH = `${repoFolder}/languages`;
process.env.TIDDLYWIKI_EDITION_PATH = `${repoFolder}/editions`;

const htmlName = 'index.html';
const minify = true;
const excludeFilter = '-[is[draft]]';

// 清空生成目标
await $`rm -rf ${distDir}`;

// 静态资源拷贝
await $`cp -r public/ ${distDir} &> /dev/null`;
await $`cp tiddlers/favicon.ico ${distDir}/favicon.ico &> /dev/null`;
await $`cp vercel.json ${distDir}/vercel.json &> /dev/null`;

// 构建HTML
await $`npx tiddlywiki . \\
--output ${distDir} \\
--build deletetiddlers externalimages externaltext externaljs \\
--render $:/core/save/offline-external-js index-raw.html text/plain "" publishFilter ${excludeFilter} \\
`;

// 最小化:核心JS和HTML
// 最小化:核心JS和HTML
const o_tw_core = `${distDir}/tiddlywikicore.js`;
const r_tw_core = `${distDir}/tiddlywikicore-${tw_Version}.js`;
if (minify) {
await $`npx uglifyjs ${o_tw_core} -c -m --v8 --webkit --ie --output ${r_tw_core}`;
await $`rm ${o_tw_core}`;
await $`npx html-minifier-terser -c scripts/html-minifier-terser.config.json -o ${distDir}/${htmlName} ${distDir}/index-raw.html`;
await $`rm ${distDir}/index-raw.html`;
} else {
await $`mv ${o_tw_core} ${r_tw_core}`;
await $`mv ${distDir}/index-raw.html ${distDir}/${htmlName}`;
}
25 changes: 13 additions & 12 deletions scripts/build-wiki.mjs → scripts/test/build-wiki.mjs
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import { fs } from 'zx';
import path from 'path';
import { fs } from "zx";
import path from "path";

const repoFolder = path.join(path.dirname(__filename), '..');
const folderToServe = path.join(repoFolder, 'public-dist');
const repoFolder = path.join(path.dirname(__filename), "..");
const folderToServe = path.join(repoFolder, "public-dist");

// cross-env TIDDLYWIKI_PLUGIN_PATH='node_modules/tiddlywiki/plugins/published' TIDDLYWIKI_THEME_PATH='${wikiFolderName}/themes'
process.env.TIDDLYWIKI_PLUGIN_PATH = `${repoFolder}/plugins`;
process.env.TIDDLYWIKI_THEME_PATH = `${repoFolder}/themes`;

// npm run build:prepare
await $`rm -rf ${folderToServe}`;
// npm run build:public
await $`cp -r ${repoFolder}/public/ ${folderToServe}`;

try {
await $`rm -rf ${folderToServe}`;
await $`cp -r ${repoFolder}/public/ ${folderToServe}`;
await $`cp ${repoFolder}/vercel.json ${folderToServe}/vercel.json`;
} catch (error) {
console.log(error);
}
// try copy some static assets, don't cause error if some of them been removed by the user
try {
// npm run build:public
await $`cp ${repoFolder}/tiddlers/favicon.ico ${folderToServe}/favicon.ico`;
} catch (error) {
console.log(error);
}
// npm run build:nodejs2html

// exclude edit related plugins, make it readonly, and reduce size
await $`tiddlywiki ${repoFolder} --build readonlyexternalimages`;
await $`tiddlywiki ${repoFolder} --build externaljs`;
Expand All @@ -35,8 +33,11 @@ const htmlOutputPath = `${folderToServe}/index.html`;
await $`html-minifier-terser -c ./scripts/html-minifier-terser.config.json -o ${htmlMinifyPath} ${repoFolder}/output/index.html`;
// build dll.js and config tw to load it
// original filename contains invalid char, will cause static server unable to load it
const htmlContent = fs.readFileSync(htmlMinifyPath, 'utf-8');
const htmlContentWithCorrectJsPath = htmlContent.replaceAll('%24%3A%2Fcore%2Ftemplates%2Ftiddlywiki5.js', 'tiddlywiki5.js');
const htmlContent = fs.readFileSync(htmlMinifyPath, "utf-8");
const htmlContentWithCorrectJsPath = htmlContent.replaceAll(
"%24%3A%2Fcore%2Ftemplates%2Ftiddlywiki5.js",
"tiddlywiki5.js"
);
fs.writeFileSync(htmlOutputPath, htmlContentWithCorrectJsPath);
await $`mv ${repoFolder}/output/tiddlywiki5.js ${folderToServe}/tiddlywiki5.js`;
// npm run build:precache
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions tiddlers/Hi 阿狸.tid
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
title: Hi 阿狸
type: text/vnd.tiddlywiki

这是一只火狐狸
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: $:/core/templates/canonical-uri-external-text
type: text/vnd.tiddlywiki

<!--

This template is used to assign the ''_canonical_uri'' field to external text files.

Change the `./text/` part to a different base URI. The URI can be relative or absolute.

-->
https://raw.githubusercontent.com/{{$:/GitHub/Repo}}/{{$:/GitHub/Branch}}/{{$:/config/linonetwo/github-external-image/tiddler-folder-name}}/<$view field="title" format="doubleurlencoded"/>.tid
Loading

0 comments on commit ac6a0d4

Please sign in to comment.