>您可能熟悉Jekyll和Wintersmith等靜態站點生成器。 本教程演示了構建自定義grunt插件,以從模板和降價文件中生成靜態站點,從而通過現有工作流提供了更大的控制和集成。
使用Grunt的關鍵優勢:
>
> install:
安裝Grunt插件模板:
grunt-init
)。 npm install -g grunt-init
>
git clone git://github.com/gruntjs/grunt-init-gruntplugin.git ~/.grunt-init/gruntplugin
grunt-mini-static-blog
grunt-init gruntplugin
npm install
生成博客文章:npm install handlebars highlight.js meta-marked moment rss lodash --save-dev
包括以下配置創建必要的模板文件(,
,,Gruntfile.js
,mini_static_blog
,
<code class="language-javascript">mini_static_blog: { default: { options: { data: { author: "Your Name", url: "http://yourwebsite.com", disqus: "", // Disqus username (optional) title: 'My Blog', description: 'My Blog Description' }, template: { post: 'templates/post.hbs', page: 'templates/page.hbs', index: 'templates/index.hbs', header: 'templates/partials/header.hbs', footer: 'templates/partials/footer.hbs', notfound: 'templates/404.hbs' }, src: { posts: 'content/posts/', pages: 'content/pages/' }, www: { dest: 'build' } } } }</code>
進一步的開發: 這個插件構成了更全面的靜態站點生成器的基礎。 考慮以下這些增強: >本教程提供了一個可靠的起點,用於使用Grunt創建自定義的靜態站點生成器。 請記住諮詢完整的代碼(可在[鏈接到源代碼 - 如果可用時替換為實際鏈接)以進行詳細實現。 templates/post.hbs
插件代碼(mini_static_blog.js):templates/page.hbs
templates/index.hbs
templates/partials/header.hbs
核心邏輯位於templates/partials/footer.hbs
內。 該代碼處理Markdown解析,模板渲染和文件編寫。 本節包括使用語法突出顯示,模板編譯,後期,頁面生成,RSS Feed創建和404頁創建的降級處理。 該代碼還管理索引頁面的分頁。 (完整的,詳細的代碼太廣泛,無法在此處包含,但是上面描述了結構和關鍵元素。)
以上是構建一個靜態站點發電機GRUNT插件的詳細內容。更多資訊請關注PHP中文網其他相關文章!