亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
關(guān)鍵要點(diǎn)
為什么選擇 Metalsmith?
安裝 Metalsmith
項(xiàng)目結(jié)構(gòu)
頁面
模板
部分
靜態(tài)資產(chǎn)
自定義插件
構(gòu)建目錄
定義您的第一個(gè)構(gòu)建文件
Metalsmith 插件
制作更好的構(gòu)建
需要注意的地方
不兼容的插件
插件順序至關(guān)重要
Browsersync 重新構(gòu)建問題
您是否仍然需要 Gulp?
Metalsmith 適合您嗎?
關(guān)于使用 Metalsmith 創(chuàng)建靜態(tài)網(wǎng)站的常見問題
什么是 Metalsmith,為什么我應(yīng)該使用它來創(chuàng)建靜態(tài)網(wǎng)站?
如何安裝 Metalsmith?
如何創(chuàng)建一個(gè)新的 Metalsmith 項(xiàng)目?
如何在我的 Metalsmith 項(xiàng)目中添加插件?
如何構(gòu)建我的 Metalsmith 網(wǎng)站?
如何自定義我的 Metalsmith 網(wǎng)站的布局?
如何在我的 Metalsmith 網(wǎng)站中添加內(nèi)容?
如何部署我的 Metalsmith 網(wǎng)站?
如何更新我的 Metalsmith 網(wǎng)站?
我可以將 Metalsmith 用于大型復(fù)雜網(wǎng)站嗎?
首頁 web前端 js教程 如何使用金屬史密斯創(chuàng)建靜態(tài)站點(diǎn)

如何使用金屬史密斯創(chuàng)建靜態(tài)站點(diǎn)

Feb 18, 2025 am 09:00 AM

How to Create a Static Site with Metalsmith

之前文章探討了是否應(yīng)該使用靜態(tài)網(wǎng)站生成器的理由。簡而言之,靜態(tài)網(wǎng)站生成器從模板和原始數(shù)據(jù)(通常包含在 Markdown 文件中)構(gòu)建僅包含 HTML 的頁面文件。它提供了一些 CMS 的好處,而沒有托管、性能和安全方面的開銷。

靜態(tài)網(wǎng)站可能適合各種項(xiàng)目,包括:

  • 小型網(wǎng)站或個(gè)人博客。擁有幾十個(gè)頁面、不頻繁的帖子和一兩個(gè)作者的網(wǎng)站可能是理想的選擇。
  • 技術(shù)文檔,例如 REST API。
  • 需要一系列網(wǎng)頁視圖的應(yīng)用程序原型。
  • 電子書——Markdown 文件可以轉(zhuǎn)換為 PDF 或其他格式以及 HTML。

本質(zhì)上,靜態(tài)網(wǎng)站生成器是一個(gè)構(gòu)建工具。您可以像使用 Grunt 或 Gulp 一樣,使用它來運(yùn)行任務(wù)或項(xiàng)目腳手架。

關(guān)鍵要點(diǎn)

  • Metalsmith 是一款靈活的可插入靜態(tài)網(wǎng)站生成器,非常適合創(chuàng)建輕量級(jí)網(wǎng)站,而無需傳統(tǒng) CMS 的開銷。
  • Metalsmith 的安裝和設(shè)置需要 Node.js,此過程涉及初始化新的項(xiàng)目目錄并通過 npm 安裝必要的插件。
  • Metalsmith 中的項(xiàng)目結(jié)構(gòu)涉及組織源文件、模板和資產(chǎn),并明確區(qū)分開發(fā)和生產(chǎn)構(gòu)建。
  • Metalsmith 利用各種插件來擴(kuò)展功能,例如處理 Markdown 文件、創(chuàng)建 RSS 提要和生成站點(diǎn)地圖,這些都配置在構(gòu)建文件中。
  • 可以創(chuàng)建自定義插件來處理特定任務(wù),例如設(shè)置元數(shù)據(jù)或添加調(diào)試信息,從而增強(qiáng) Metalsmith 在管理網(wǎng)站內(nèi)容和結(jié)構(gòu)方面的多功能性。
  • Metalsmith 的構(gòu)建過程可以與 Gulp 等任務(wù)運(yùn)行器集成,以應(yīng)對(duì)更復(fù)雜的場景,盡管對(duì)于更簡單的流程,Metalsmith 本身就足夠了。

為什么選擇 Metalsmith?

無可爭議的靜態(tài)網(wǎng)站冠軍是 Jekyll——一個(gè) 2008 年啟動(dòng)的 Ruby 項(xiàng)目。您不一定需要 Ruby 專業(yè)知識(shí)來使用 Jekyll,但這會(huì)有所幫助。幸運(yùn)的是,大多數(shù)流行語言都有各種各樣的開源靜態(tài)網(wǎng)站生成器。JavaScript 選項(xiàng)包括 Hexo、Harp 和 Assemble。對(duì)于更簡單的項(xiàng)目,您也可以使用 Gulp 等構(gòu)建工具。

我選擇 Metalsmith 用于本教程是因?yàn)樗?/p>

  1. 不針對(duì)特定類型的項(xiàng)目(例如博客)
  2. 支持各種模板和數(shù)據(jù)格式選項(xiàng)
  3. 輕量級(jí)
  4. 依賴項(xiàng)很少
  5. 使用模塊化結(jié)構(gòu)
  6. 提供簡單的插件架構(gòu),以及
  7. 易于上手。

本教程已經(jīng)構(gòu)建了一個(gè)演示網(wǎng)站。它不會(huì)贏得任何設(shè)計(jì)獎(jiǎng)項(xiàng),但它說明了基本概念。Metalsmith 構(gòu)建代碼可以從 GitHub 存儲(chǔ)庫中檢查和安裝?;蛘撸梢园凑沾颂幍恼f明操作并創(chuàng)建您自己的基本站點(diǎn)。

我已經(jīng)使用過幾次 Metalsmith 了——請(qǐng)不要認(rèn)為這是構(gòu)建每個(gè)靜態(tài)網(wǎng)站的最終方法!

安裝 Metalsmith

確保您已安裝 Node.js(例如使用 nvm),然后創(chuàng)建一個(gè)新的項(xiàng)目目錄,例如 project 并初始化您的 package.json 文件:

<code>cd project && cd project
npm init -y
</code>

現(xiàn)在安裝 Metalsmith 和我們將用于構(gòu)建站點(diǎn)的各種插件。這些是:

  • metalsmith-assets — 在您的 Metalsmith 構(gòu)建中包含靜態(tài)資產(chǎn)
  • metalsmith-browser-sync — 將 BrowserSync 集成到您的工作流程中
  • metalsmith-collections — 將文件集合添加到全局元數(shù)據(jù)
  • metalsmith-feed — 為集合生成 RSS 提要
  • metalsmith-html-minifier — 使用 kangax/html-minifier 壓縮 HTML 文件
  • metalsmith-in-place — 在源文件中呈現(xiàn)模板語法
  • metalsmith-layouts — 將布局應(yīng)用于您的源文件
  • metalsmith-mapsite — 生成 sitemap.xml 文件
  • metalsmith-markdown — 轉(zhuǎn)換 Markdown 文件
  • metalsmith-permalinks — 將自定義永久鏈接模式應(yīng)用于文件
  • metalsmith-publish — 支持草稿、私有和未來日期的帖子
  • metalsmith-word-count — 計(jì)算 HTML 文件中所有段落的字?jǐn)?shù)/平均閱讀時(shí)間
<code>npm install --save-dev metalsmith metalsmith-assets metalsmith-browser-sync metalsmith-collections metalsmith-feed metalsmith-html-minifier metalsmith-in-place metalsmith-layouts metalsmith-mapsite metalsmith-markdown metalsmith-permalinks metalsmith-publish metalsmith-word-count handlebars
</code>

項(xiàng)目結(jié)構(gòu)

我們將在項(xiàng)目中使用以下結(jié)構(gòu)作為源 (src) 和構(gòu)建 (build) 目錄。

您可以按照以下說明創(chuàng)建示例文件,也可以直接從演示 src 目錄復(fù)制它們。

頁面

頁面 Markdown 文件包含在 src/html 中。這可以包含每個(gè)網(wǎng)站部分的一級(jí)子目錄,即

  • src/html/start — 按特定順序描述項(xiàng)目的頁面
  • src/html/article — 按反時(shí)間順序排列的各種文章
  • src/html/contact — 單個(gè)聯(lián)系頁面

每個(gè)目錄包含一個(gè) index.md 文件,它是該部分的默認(rèn)頁面。其他頁面可以使用任何唯一名稱。

構(gòu)建過程會(huì)將這些文件轉(zhuǎn)換為基于目錄的永久鏈接,例如

  • src/html/start/index.md 變成 /start/index.html
  • src/html/start/installation.md 變成 /start/installation/index.html

每個(gè) Markdown 文件都提供內(nèi)容和元信息,稱為“前題”位于 --- 標(biāo)記之間的頂部,例如

<code>---
title: My page title
description: A description of this page.
layout: page.html
priority: 0.9
date: 2016-04-19
publish: draft
---

This is a demonstration page.

## Example title
Body text.</code>

大多數(shù)前題都是可選的,但您可以設(shè)置:

  • priority:介于 0(低)和 1(高)之間的數(shù)字,我們將使用它來排序菜單和定義 XML 站點(diǎn)地圖。
  • publish:可以設(shè)置為草稿、私有或未來日期,以確保它不會(huì)在需要之前發(fā)布。
  • date:文章的日期。如果沒有設(shè)置,我們將使用任何未來的發(fā)布日期或文件創(chuàng)建日期。
  • layout:要使用的 HTML 模板。

模板

HTML 頁面模板包含在 src/template 中。已定義兩個(gè)模板:

  • src/html/template/page.html 默認(rèn)布局
  • src/html/template/article.md 顯示日期、前后鏈接等的文章布局

雖然支持其他選項(xiàng),但使用了 Handlebars 模板系統(tǒng)。典型的模板需要一個(gè) {{{ contents }}} 標(biāo)記來包含頁面內(nèi)容以及任何前題值,例如 {{ title }}:

<code>cd project && cd project
npm init -y
</code>

對(duì) {{> meta }}、{{> header }} 和 {{> footer }} 的引用是部分……

部分

部分——或 HTML 代碼片段文件——包含在 src/partials 中。這些主要用于模板中,但也可以使用以下代碼包含在內(nèi)容頁面中:

<code>npm install --save-dev metalsmith metalsmith-assets metalsmith-browser-sync metalsmith-collections metalsmith-feed metalsmith-html-minifier metalsmith-in-place metalsmith-layouts metalsmith-mapsite metalsmith-markdown metalsmith-permalinks metalsmith-publish metalsmith-word-count handlebars
</code>

其中 partialname 是 src/partials 目錄中文件的名稱。

靜態(tài)資產(chǎn)

靜態(tài)資產(chǎn)(例如圖像、CSS 和 JavaScript 文件)包含在 src/assets 中。所有文件和子目錄都將按原樣復(fù)制到網(wǎng)站的根目錄。

自定義插件

構(gòu)建站點(diǎn)所需的自定義插件包含在 lib 目錄中。

構(gòu)建目錄

網(wǎng)站將在構(gòu)建目錄中構(gòu)建。我們將通過兩種方式構(gòu)建網(wǎng)站:

  • 開發(fā)模式:HTML 不會(huì)被壓縮,并且會(huì)啟動(dòng)測試 Web 服務(wù)器。
  • 生產(chǎn)模式:如果 NODE_ENV 設(shè)置為 production,則會(huì)清除構(gòu)建目錄并生成最終壓縮的文件。

定義您的第一個(gè)構(gòu)建文件

可以在項(xiàng)目目錄的根目錄中創(chuàng)建一個(gè)名為 build.js 的基本示例:

<code>---
title: My page title
description: A description of this page.
layout: page.html
priority: 0.9
date: 2016-04-19
publish: draft
---

This is a demonstration page.

## Example title
Body text.</code>

使用 node ./build.js 運(yùn)行它,一個(gè)靜態(tài)網(wǎng)站將在構(gòu)建目錄中創(chuàng)建。Markdown 將被解析為 HTML,但它不可用,因?yàn)槲覀儧]有在構(gòu)建過程中包含模板。

Metalsmith 插件

從表面上看,Metalsmith 構(gòu)建文件看起來類似于 Gulp 中使用的構(gòu)建文件(盡管它不使用流)。通過使用任何適當(dāng)?shù)膮?shù)將其傳遞給 Metalsmith use 方法來調(diào)用插件。插件本身必須返回另一個(gè)函數(shù),該函數(shù)接受三個(gè)參數(shù):

  • 一個(gè) files 數(shù)組,包含有關(guān)每個(gè)頁面的信息
  • 一個(gè) metalsmith 對(duì)象,包含全局信息,例如元數(shù)據(jù),以及
  • 一個(gè) done 函數(shù),在插件完成工作后必須調(diào)用該函數(shù)

這個(gè)簡單的示例將所有元數(shù)據(jù)和頁面信息記錄到控制臺(tái)(它可以在 build.js 中定義):

<code>
 lang="en">
  >
    {{> meta }}
  >
  >

  {{> header }}

  <main>></main>
    >

      {{#if title}}
        <h1>></h1>{{ title }}>
      {{/if}}

      {{{ contents }}}

    >
  >

  {{> footer }}

>
>
</code>

Metalsmith 構(gòu)建代碼可以更新為使用此插件:

<code>{{> partialname }}</code>

此調(diào)試函數(shù)可以幫助您創(chuàng)建自己的自定義插件,但是您可能需要的絕大多數(shù)功能都已經(jīng)編寫好了——Metalsmith 網(wǎng)站上列出了很長的插件列表。

制作更好的構(gòu)建

下面解釋了演示站點(diǎn)構(gòu)建文件的主要部分。

如果 NODE_ENV 環(huán)境變量已設(shè)置為 production(在 Mac/Linux 上導(dǎo)出 NODE_ENV=production 或在 Windows 上設(shè)置 NODE_ENV=production),則變量 devBuild 將設(shè)置為 true:

<code>cd project && cd project
npm init -y
</code>

主目錄在 dir 對(duì)象中定義,以便我們可以重復(fù)使用它們:

<code>npm install --save-dev metalsmith metalsmith-assets metalsmith-browser-sync metalsmith-collections metalsmith-feed metalsmith-html-minifier metalsmith-in-place metalsmith-layouts metalsmith-mapsite metalsmith-markdown metalsmith-permalinks metalsmith-publish metalsmith-word-count handlebars
</code>

加載 Metalsmith 和插件模塊。注意:

  • 優(yōu)秀的 Browsersync 測試服務(wù)器僅在創(chuàng)建開發(fā)構(gòu)建時(shí)才需要
  • htmlmin 引用的 HTML 壓縮程序模塊僅在創(chuàng)建生產(chǎn)構(gòu)建時(shí)才需要
  • 已定義三個(gè)自定義插件:setdate、moremeta 和 debug(下面將更詳細(xì)地解釋)
<code>---
title: My page title
description: A description of this page.
layout: page.html
priority: 0.9
date: 2016-04-19
publish: draft
---

This is a demonstration page.

## Example title
Body text.</code>

siteMeta 對(duì)象使用應(yīng)用于每個(gè)頁面的信息定義。重要的值是 domain 和 rootpath,它們根據(jù)開發(fā)或生產(chǎn)構(gòu)建進(jìn)行設(shè)置:

<code>
 lang="en">
  >
    {{> meta }}
  >
  >

  {{> header }}

  <main>></main>
    >

      {{#if title}}
        <h1>></h1>{{ title }}>
      {{/if}}

      {{{ contents }}}

    >
  >

  {{> footer }}

>
>
</code>

還定義了一個(gè) templateConfig 對(duì)象來設(shè)置模板默認(rèn)值。這將由 metalsmith-in-place 和 metalsmith-layouts 插件使用,它們啟用使用 Handlebars 的頁面內(nèi)和模板渲染:

<code>{{> partialname }}</code>

Metalsmith 對(duì)象現(xiàn)在像以前一樣初始化,但我們還將 siteMeta 對(duì)象傳遞給 metadata 方法,以確保該信息可用于每個(gè)頁面。因此,我們可以在任何頁面中引用諸如 {{ name }} 之類的項(xiàng)目以獲取站點(diǎn)名稱。

<code>// basic build

'use strict';

var
  metalsmith = require('metalsmith'),
  markdown   = require('metalsmith-markdown'),

  ms = metalsmith(__dirname) // the working directory
    .clean(true)            // clean the build directory
    .source('src/html/')    // the page source directory
    .destination('build/')  // the destination directory
    .use(markdown())        // convert markdown to HTML
    .build(function(err) {  // build the site
      if (err) throw err;   // and throw errors
    });
</code>

我們的第一個(gè)插件調(diào)用調(diào)用 metalsmith-publish,它刪除任何其前題 publish 值設(shè)置為草稿、私有或未來日期的文件:

<code>function debug(logToConsole) {
  return function(files, metalsmith, done) {
    if (logToConsole) {
      console.log('\nMETADATA:');
      console.log(metalsmith.metadata());

      for (var f in files) {
        console.log('\nFILE:');
        console.log(files[f]);
      }
    }

    done();
  };
};
</code>

setdate 是包含在 lib/metalsmith-setdate.js 中的自定義插件。它確保每個(gè)文件都設(shè)置了“date”值,即使在前題中沒有定義任何值,也可以通過盡可能回退到發(fā)布日期或文件創(chuàng)建時(shí)間來實(shí)現(xiàn):

<code>ms = metalsmith(__dirname) // the working directory
  .clean(true)             // clean the build directory
  .source('src/html/')     // the page source directory
  .destination('build/')   // the destination directory
  .use(markdown())         // convert Markdown to HTML
  .use(debug(true))        // *** NEW *** output debug information
  .build(function(err) {   // build the site
    if (err) throw err;    // and throw errors
  });
</code>

metalsmith-collections 是最重要的插件之一,因?yàn)樗鶕?jù)頁面在源目錄中的位置或其他因素將每個(gè)頁面分配給類別或分類法。它可以使用前題(例如日期或優(yōu)先級(jí))重新排序文件,并允許您為該集合設(shè)置自定義元數(shù)據(jù)。代碼定義:

  • src/html/start 目錄中每個(gè)文件的 start 集合。它按文件中前題中設(shè)置的優(yōu)先級(jí)值對(duì)它們進(jìn)行排序。
  • src/html/article 目錄中每個(gè)文件的 article 集合。它按反時(shí)間順序?qū)λ鼈冞M(jìn)行排序
  • 每個(gè)名為 index.* 的默認(rèn)頁面的 page 集合。它按文件中前題中設(shè)置的優(yōu)先級(jí)值對(duì)它們進(jìn)行排序。
<code>devBuild = ((process.env.NODE_ENV || '').trim().toLowerCase() !== 'production')
</code>

接下來是 Markdown 到 HTML 的轉(zhuǎn)換,然后是 metalsmith-permalinks 插件,它為構(gòu)建定義了目錄結(jié)構(gòu)。請(qǐng)注意,moremeta 在下面為每個(gè)文件設(shè)置了 :mainCollection:

<code>dir = {
  base:   __dirname + '/',
  lib:    __dirname + '/lib/',
  source: './src/',
  dest:   './build/'
}
</code>

metalsmith-word-count 計(jì)算文章中的字?jǐn)?shù)并計(jì)算大約需要多長時(shí)間才能閱讀。參數(shù) { raw: true } 僅輸出數(shù)字:

<code>metalsmith  = require('metalsmith'),
markdown    = require('metalsmith-markdown'),
publish     = require('metalsmith-publish'),
wordcount   = require("metalsmith-word-count"),
collections = require('metalsmith-collections'),
permalinks  = require('metalsmith-permalinks'),
inplace     = require('metalsmith-in-place'),
layouts     = require('metalsmith-layouts'),
sitemap     = require('metalsmith-mapsite'),
rssfeed     = require('metalsmith-feed'),
assets      = require('metalsmith-assets'),
htmlmin     = devBuild ? null : require('metalsmith-html-minifier'),
browsersync = devBuild ? require('metalsmith-browser-sync') : null,

// custom plugins
setdate     = require(dir.lib + 'metalsmith-setdate'),
moremeta    = require(dir.lib + 'metalsmith-moremeta'),
debug       = consoleLog ? require(dir.lib + 'metalsmith-debug') : null,
</code>

moremeta 是包含在 lib/metalsmith-moremeta.js 中的另一個(gè)自定義插件。它將其他元數(shù)據(jù)附加到每個(gè)文件:

  • root:到根目錄的絕對(duì)或計(jì)算出的相對(duì)文件路徑
  • isPage:對(duì)于名為 index.* 的默認(rèn)部分頁面設(shè)置為 true
  • mainCollection:主要集合名稱,即 start 或 article
  • layout:如果未設(shè)置,則可以從主集合的元數(shù)據(jù)中確定布局模板
  • navmain:頂級(jí)導(dǎo)航對(duì)象的數(shù)組
  • navsub:二級(jí)導(dǎo)航對(duì)象的數(shù)組

插件代碼比較復(fù)雜,因?yàn)樗幚韺?dǎo)航。如果您需要更簡單的層次結(jié)構(gòu),則有更簡單的選項(xiàng)。

<code>cd project && cd project
npm init -y
</code>

metalsmith-in-place 和 metalsmith-layouts 插件分別控制頁面內(nèi)和模板布局。傳遞上面定義的相同 templateConfig 對(duì)象:

<code>npm install --save-dev metalsmith metalsmith-assets metalsmith-browser-sync metalsmith-collections metalsmith-feed metalsmith-html-minifier metalsmith-in-place metalsmith-layouts metalsmith-mapsite metalsmith-markdown metalsmith-permalinks metalsmith-publish metalsmith-word-count handlebars
</code>

如果設(shè)置了 htmlmin(在生產(chǎn)構(gòu)建中),我們可以壓縮 HTML:

<code>---
title: My page title
description: A description of this page.
layout: page.html
priority: 0.9
date: 2016-04-19
publish: draft
---

This is a demonstration page.

## Example title
Body text.</code>

debug 是包含在 lib/metalsmith-debug.js 中的最終自定義插件。它類似于上面描述的 debug 函數(shù):

<code>
 lang="en">
  >
    {{> meta }}
  >
  >

  {{> header }}

  <main>></main>
    >

      {{#if title}}
        <h1>></h1>{{ title }}>
      {{/if}}

      {{{ contents }}}

    >
  >

  {{> footer }}

>
>
</code>

啟動(dòng) Browsersync 測試服務(wù)器,以便我們可以測試開發(fā)構(gòu)建。如果您以前沒有使用過它,它看起來就像魔法:每次您進(jìn)行更改時(shí),您的網(wǎng)站都會(huì)神奇地刷新,并且當(dāng)您滾動(dòng)或?yàn)g覽網(wǎng)站時(shí),兩個(gè)或多個(gè)瀏覽器中的視圖會(huì)同步:

<code>{{> partialname }}</code>

最后,我們可以使用:

  • metalsmith-mapsite 生成 XML 站點(diǎn)地圖
  • metalsmith-feed 為 article 集合中的頁面生成 RSS 提要
  • metalsmith-assets 直接將 src/assets 中的文件和目錄復(fù)制到 build,而無需修改。
<code>// basic build

'use strict';

var
  metalsmith = require('metalsmith'),
  markdown   = require('metalsmith-markdown'),

  ms = metalsmith(__dirname) // the working directory
    .clean(true)            // clean the build directory
    .source('src/html/')    // the page source directory
    .destination('build/')  // the destination directory
    .use(markdown())        // convert markdown to HTML
    .build(function(err) {  // build the site
      if (err) throw err;   // and throw errors
    });
</code>

剩下的就是最后的 .build() 步驟來創(chuàng)建網(wǎng)站:

<code>function debug(logToConsole) {
  return function(files, metalsmith, done) {
    if (logToConsole) {
      console.log('\nMETADATA:');
      console.log(metalsmith.metadata());

      for (var f in files) {
        console.log('\nFILE:');
        console.log(files[f]);
      }
    }

    done();
  };
};
</code>

完成后,您可以再次運(yùn)行 node ./build.js 來構(gòu)建您的靜態(tài)網(wǎng)站。

需要注意的地方

我在構(gòu)建簡單的 Metalsmith 網(wǎng)站時(shí)學(xué)到了很多東西,但請(qǐng)注意以下問題:

不兼容的插件

插件可能會(huì)與其他插件沖突。例如,計(jì)算相對(duì)根路徑的 metalsmith-rootpath 與創(chuàng)建自定義構(gòu)建目錄結(jié)構(gòu)的 metalsmith-permalinks 不太兼容。我通過在 lib/metalsmith-moremeta.js 插件中編寫自定義根路徑計(jì)算代碼解決了此問題。

插件順序至關(guān)重要

如果插件放置的順序錯(cuò)誤,則插件可能會(huì)相互依賴或沖突。例如,生成 RSS 的 metalsmith-feed 插件必須在 metalsmith-layouts 之后調(diào)用,以確保不會(huì)在頁面模板中生成 RSS XML。

Browsersync 重新構(gòu)建問題

當(dāng) Browsersync 運(yùn)行并編輯文件時(shí),集合會(huì)被重新解析,但舊數(shù)據(jù)似乎仍然存在。這可能是 lib/metalsmith-moremeta.js 自定義插件的問題,但菜單和前后鏈接可能會(huì)不同步。要修復(fù)它,請(qǐng)使用 Ctrl/Cmd C 停止構(gòu)建并重新啟動(dòng)構(gòu)建。

您是否仍然需要 Gulp?

使用 Gulp 等任務(wù)管理器的人會(huì)注意到 Metalsmith 提供了一個(gè)熟悉的構(gòu)建過程。有用于使用 Sass 進(jìn)行 CSS 預(yù)處理、圖像壓縮、文件連接、丑化和更多功能的插件。對(duì)于更簡單的流程,它可能就足夠了。

但是,Gulp 具有更廣泛的插件范圍,并允許進(jìn)行復(fù)雜的構(gòu)建活動(dòng),例如 lint、部署和使用 auto-prefixer 的 PostCSS 處理。有一些 Gulp/Metalsmith 集成插件,盡管我遇到了一些問題,并且它們不應(yīng)該是必要的,因?yàn)?Gulp 任務(wù)可以直接運(yùn)行 Metalsmith,例如

<code>cd project && cd project
npm init -y
</code>

此過程可以防止上面提到的 Browsersync 重新構(gòu)建問題。請(qǐng)記住使用 .clean(false) 以確保當(dāng)其他任務(wù)處于活動(dòng)狀態(tài)時(shí),Metalsmith 永遠(yuǎn)不會(huì)清除構(gòu)建文件夾。

Metalsmith 適合您嗎?

如果您有簡單或高度定制的網(wǎng)站需求,Metalsmith 是理想的選擇。也許可以嘗試使用文檔項(xiàng)目并一次添加一個(gè)功能。Metalsmith 并不像 Jekyll 等替代方案那樣功能齊全,但它并非旨在如此。您可能需要編寫自己的插件,但這樣做很容易,這對(duì) JavaScript 開發(fā)人員來說是一個(gè)巨大的好處。

創(chuàng)建 Metalsmith 構(gòu)建系統(tǒng)需要時(shí)間,我們還沒有考慮 HTML 模板和部署所涉及的工作量。但是,一旦您擁有一個(gè)可用的流程,添加、編輯和刪除 Markdown 文件就會(huì)變得非常簡單。它可能比使用 CMS 更容易,并且您擁有靜態(tài)網(wǎng)站的所有好處。

關(guān)于使用 Metalsmith 創(chuàng)建靜態(tài)網(wǎng)站的常見問題

什么是 Metalsmith,為什么我應(yīng)該使用它來創(chuàng)建靜態(tài)網(wǎng)站?

Metalsmith 是一款簡單易用的可插入靜態(tài)網(wǎng)站生成器。它基于 Node.js,并使用模塊化結(jié)構(gòu),允許您通過插件根據(jù)需要添加功能。這使其具有令人難以置信的靈活性和可定制性。您應(yīng)該使用 Metalsmith 創(chuàng)建靜態(tài)網(wǎng)站,因?yàn)樗试S您完全按照自己的意愿構(gòu)建網(wǎng)站,而無需受傳統(tǒng) CMS 的限制。此外,靜態(tài)網(wǎng)站比動(dòng)態(tài)網(wǎng)站更快、更安全且更易于維護(hù)。

如何安裝 Metalsmith?

要安裝 Metalsmith,您需要在計(jì)算機(jī)上安裝 Node.js 和 npm。安裝這些后,您可以通過在終端中運(yùn)行命令 npm install metalsmith 來安裝 Metalsmith。這將安裝 Metalsmith 及其所有依賴項(xiàng)。

如何創(chuàng)建一個(gè)新的 Metalsmith 項(xiàng)目?

要?jiǎng)?chuàng)建一個(gè)新的 Metalsmith 項(xiàng)目,首先在終端中導(dǎo)航到要?jiǎng)?chuàng)建項(xiàng)目的目錄。然后,運(yùn)行命令 metalsmith 創(chuàng)建一個(gè)新項(xiàng)目。這將創(chuàng)建一個(gè)新的目錄,其名稱為您的項(xiàng)目名稱,在此目錄內(nèi),它將為您的靜態(tài)網(wǎng)站創(chuàng)建一個(gè)基本結(jié)構(gòu)。

如何在我的 Metalsmith 項(xiàng)目中添加插件?

要向您的 Metalsmith 項(xiàng)目添加插件,您需要通過 npm 安裝它們,然后在您的 Metalsmith 配置文件中引用它們。例如,要添加 markdown 插件,您首先需要運(yùn)行 npm install metalsmith-markdown,然后在您的配置文件中,您需要添加 var markdown = require('metalsmith-markdown'); 并將 .use(markdown()) 添加到您的 Metalsmith 構(gòu)建鏈中。

如何構(gòu)建我的 Metalsmith 網(wǎng)站?

要構(gòu)建您的 Metalsmith 網(wǎng)站,您需要在終端中運(yùn)行命令 metalsmith build。這將編譯所有文件并將它們輸出到構(gòu)建目錄,然后您可以將其部署到您的服務(wù)器。

如何自定義我的 Metalsmith 網(wǎng)站的布局?

要自定義您的 Metalsmith 網(wǎng)站的布局,您可以使用 Handlebars 或 Jade 等模板引擎。這些允許您為網(wǎng)站的不同部分(如頁眉、頁腳和各個(gè)頁面)創(chuàng)建可重用的模板。

如何在我的 Metalsmith 網(wǎng)站中添加內(nèi)容?

要向您的 Metalsmith 網(wǎng)站添加內(nèi)容,您可以在源目錄中創(chuàng)建 markdown 文件。構(gòu)建網(wǎng)站時(shí),這些文件將轉(zhuǎn)換為 HTML。您還可以使用 Netlify CMS 等 CMS 來管理您的內(nèi)容。

如何部署我的 Metalsmith 網(wǎng)站?

要部署您的 Metalsmith 網(wǎng)站,您可以使用 Netlify 或 GitHub Pages 等服務(wù)。這些服務(wù)將托管您的靜態(tài)網(wǎng)站,并在您推送到存儲(chǔ)庫時(shí)自動(dòng)部署更改。

如何更新我的 Metalsmith 網(wǎng)站?

要更新您的 Metalsmith 網(wǎng)站,您只需更改源文件,然后重新構(gòu)建您的網(wǎng)站。更改將反映在構(gòu)建目錄中,然后您可以將其部署到您的服務(wù)器。

我可以將 Metalsmith 用于大型復(fù)雜網(wǎng)站嗎?

是的,Metalsmith 具有高度可擴(kuò)展性,可用于大型復(fù)雜網(wǎng)站。其模塊化結(jié)構(gòu)允許您根據(jù)需要添加功能,并且它使用靜態(tài)文件意味著它可以處理大量內(nèi)容而不會(huì)減慢速度。

以上是如何使用金屬史密斯創(chuàng)建靜態(tài)站點(diǎn)的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

為什么要將標(biāo)簽放在的底部? 為什么要將標(biāo)簽放在的底部? Jul 02, 2025 am 01:22 AM

PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

什么是在DOM中冒泡和捕獲的事件? 什么是在DOM中冒泡和捕獲的事件? Jul 02, 2025 am 01:19 AM

事件捕獲和冒泡是DOM中事件傳播的兩個(gè)階段,捕獲是從頂層向下到目標(biāo)元素,冒泡是從目標(biāo)元素向上傳播到頂層。1.事件捕獲通過addEventListener的useCapture參數(shù)設(shè)為true實(shí)現(xiàn);2.事件冒泡是默認(rèn)行為,useCapture設(shè)為false或省略;3.可使用event.stopPropagation()阻止事件傳播;4.冒泡支持事件委托,提高動(dòng)態(tài)內(nèi)容處理效率;5.捕獲可用于提前攔截事件,如日志記錄或錯(cuò)誤處理。了解這兩個(gè)階段有助于精確控制JavaScript響應(yīng)用戶操作的時(shí)機(jī)和方式。

JavaScript模塊上的確定JS綜述:ES模塊與COMPORJS JavaScript模塊上的確定JS綜述:ES模塊與COMPORJS Jul 02, 2025 am 01:28 AM

ES模塊和CommonJS的主要區(qū)別在于加載方式和使用場景。1.CommonJS是同步加載,適用于Node.js服務(wù)器端環(huán)境;2.ES模塊是異步加載,適用于瀏覽器等網(wǎng)絡(luò)環(huán)境;3.語法上,ES模塊使用import/export,且必須位于頂層作用域,而CommonJS使用require/module.exports,可在運(yùn)行時(shí)動(dòng)態(tài)調(diào)用;4.CommonJS廣泛用于舊版Node.js及依賴它的庫如Express,ES模塊則適用于現(xiàn)代前端框架和Node.jsv14 ;5.雖然可混合使用,但容易引發(fā)問題

垃圾收集如何在JavaScript中起作用? 垃圾收集如何在JavaScript中起作用? Jul 04, 2025 am 12:42 AM

JavaScript的垃圾回收機(jī)制通過標(biāo)記-清除算法自動(dòng)管理內(nèi)存,以減少內(nèi)存泄漏風(fēng)險(xiǎn)。引擎從根對(duì)象出發(fā)遍歷并標(biāo)記活躍對(duì)象,未被標(biāo)記的則被視為垃圾并被清除。例如,當(dāng)對(duì)象不再被引用(如將變量設(shè)為null),它將在下一輪回收中被釋放。常見的內(nèi)存泄漏原因包括:①未清除的定時(shí)器或事件監(jiān)聽器;②閉包中對(duì)外部變量的引用;③全局變量持續(xù)持有大量數(shù)據(jù)。V8引擎通過分代回收、增量標(biāo)記、并行/并發(fā)回收等策略優(yōu)化回收效率,降低主線程阻塞時(shí)間。開發(fā)時(shí)應(yīng)避免不必要的全局引用、及時(shí)解除對(duì)象關(guān)聯(lián),以提升性能與穩(wěn)定性。

如何在node.js中提出HTTP請(qǐng)求? 如何在node.js中提出HTTP請(qǐng)求? Jul 13, 2025 am 02:18 AM

在Node.js中發(fā)起HTTP請(qǐng)求有三種常用方式:使用內(nèi)置模塊、axios和node-fetch。1.使用內(nèi)置的http/https模塊無需依賴,適合基礎(chǔ)場景,但需手動(dòng)處理數(shù)據(jù)拼接和錯(cuò)誤監(jiān)聽,例如用https.get()獲取數(shù)據(jù)或通過.write()發(fā)送POST請(qǐng)求;2.axios是基于Promise的第三方庫,語法簡潔且功能強(qiáng)大,支持async/await、自動(dòng)JSON轉(zhuǎn)換、攔截器等,推薦用于簡化異步請(qǐng)求操作;3.node-fetch提供類似瀏覽器fetch的風(fēng)格,基于Promise且語法簡單

var vs Let vs const:快速JS綜述解釋器 var vs Let vs const:快速JS綜述解釋器 Jul 02, 2025 am 01:18 AM

var、let和const的區(qū)別在于作用域、提升和重復(fù)聲明。1.var是函數(shù)作用域,存在變量提升,允許重復(fù)聲明;2.let是塊級(jí)作用域,存在暫時(shí)性死區(qū),不允許重復(fù)聲明;3.const也是塊級(jí)作用域,必須立即賦值,不可重新賦值,但可修改引用類型的內(nèi)部值。優(yōu)先使用const,需改變變量時(shí)用let,避免使用var。

JavaScript數(shù)據(jù)類型:原始與參考 JavaScript數(shù)據(jù)類型:原始與參考 Jul 13, 2025 am 02:43 AM

JavaScript的數(shù)據(jù)類型分為原始類型和引用類型。原始類型包括string、number、boolean、null、undefined和symbol,其值不可變且賦值時(shí)復(fù)制副本,因此互不影響;引用類型如對(duì)象、數(shù)組和函數(shù)存儲(chǔ)的是內(nèi)存地址,指向同一對(duì)象的變量會(huì)相互影響。判斷類型可用typeof和instanceof,但需注意typeofnull的歷史問題。理解這兩類差異有助于編寫更穩(wěn)定可靠的代碼。

如何遍歷DOM樹(例如,parentnode,children,NextElementsibling)? 如何遍歷DOM樹(例如,parentnode,children,NextElementsibling)? Jul 02, 2025 am 12:39 AM

DOM遍歷是網(wǎng)頁元素操作的基礎(chǔ),常用方法包括:1.使用parentNode獲取父節(jié)點(diǎn),可鏈?zhǔn)秸{(diào)用向上查找;2.children返回子元素集合,通過索引訪問首個(gè)或末尾子元素;3.nextElementSibling獲取下一個(gè)兄弟元素,結(jié)合previousElementSibling實(shí)現(xiàn)同級(jí)導(dǎo)航。實(shí)際應(yīng)用如動(dòng)態(tài)修改結(jié)構(gòu)、交互效果等,例如點(diǎn)擊按鈕高亮下一個(gè)兄弟節(jié)點(diǎn),掌握這些方法后復(fù)雜操作可通過組合實(shí)現(xiàn)。

See all articles