時間:2023-07-21 11:06:01 | 來源:網(wǎng)站運營
時間:2023-07-21 11:06:01 來源:網(wǎng)站運營
靜態(tài)網(wǎng)站生成器是如何工作的:在過去的幾年里,開源靜態(tài)網(wǎng)站生成器的數(shù)量增長迅速,StaticGen 上幾乎找得到每一種語言實現(xiàn)的版本。靜態(tài)網(wǎng)站相比動態(tài)網(wǎng)站具備無需依賴應用服務器,性能優(yōu)越,部署簡單等特點。特別適合生成靜態(tài)文檔,個人博客,飽受開發(fā)者的青睞。相比學會使用,掌握工作原理也尤為重要。// 讀取 markdown 內(nèi)容readFile(source, (err, data) => { console.log(data);})// 讀取配置內(nèi)容console.log(require(config))
---title: Hello World---Awesome static site generator
解析成:const result = { 'path/to/hello-world.md': { title: 'Hello World', content: new Buffer('Awesome static site generator.') }}
通常的做法是掃描文件,匹配分隔符`---`,分割內(nèi)容,對頭部用相應格式的引擎解析(e.g. YAML 格式用 yaml-parser,JSON 格式用 JSON.parse),對主體內(nèi)容,需要把 markdown 內(nèi)容轉(zhuǎn)換成帶有標簽的 HTML 片段,用于排版與應用樣式,推薦比較流行的解析器 marked 。const markedResult = { 'path/to/hello-world.md': { title: 'Hello World', content: new Buffer('Awesome static site generator.'), html: '<p>Awesome static site generator.</p>' }}
對其它一些靜態(tài)資源(e.g. css, js, png),可以引入一系列工具鏈,CSS 預處理器,編譯器,打包器,流程構(gòu)建工具,將全部資源相整合。由此,可以提供一套主題配置,甚至可以開放主題插件,豐富內(nèi)容。所有的設計出發(fā)點都基于更好的內(nèi)容寫作,而不必折騰繁瑣重復性的頁面開發(fā)工作。<html><head><%= title %></head><body> <% include partials/navbar %> <%- html %></body></html>
// EJS 渲染const ejsResult = ejs.render(str, { ...markedResult['path/to/hello-world.md']});
我們甚至可以拋棄以往模板引擎渲染這種比較傳統(tǒng)的方式,改用 MV* 框架(e.g. React, Vue, Angular),并結(jié)合服務端渲染技術,熱替換技術及其它工具鏈,打造一個更現(xiàn)代化的靜態(tài)網(wǎng)站生成器。// 生成目標文件writeFile(destination, ejsResult, err => { if (err) return console.error(err)}))
關鍵詞:工作,成器,靜態(tài)
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。