技術ブログ

工事中


handlebarsを使ってみる

インストール

# プロジェクト直下で実行
npm init --yes

# handlebarsをinstall
npm install handlebars

作成ファイル

make.js

const Handlebars = require("handlebars");
const template = Handlebars.compile("Name: {{name}}");
console.log(template({ name: "Nils" }));

package.jsonを編集

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "node make.js >> output.html"
  },

実行

npm run build

結果

Name: Nils

テンプレートを作成して埋め込む

ファイルの読み込み

const fs = require('fs');
let text = fs.readFileSync("sample.txt");

https://qiita.com/shirokuman/items/509b159bf4b8dd1c41ef

作成物にタイムスタンプを付与する

  "scripts": {
    "test": "node make.js",
    "build": "node make.js >> output_`date +%Y%m%d%H%M%S`.html"
  },

https://uchy.me/blog/20240617006/

Markdownをパースする

npm install marked

参考

https://handlebarsjs.com/installation/#npm-or-yarn-recommended