Archetypes

用于创建新内容模板的脚本。

Archetypes(原型)是用于在你的源目录中创建包含预配置内容的新文件的脚本。一个典型的例子是博客文章:当你想要创建一篇新文章时,你无需每次都从头开始创建一个新的 markdown 文件,而是可以创建并运行一个 archetype 来为你完成这项工作。

Lume 中的 archetypes 是 JavaScript 或 TypeScript 文件,存储在 _archetypes 目录中,默认导出一个函数,该函数返回一个对象,其中包含要创建的文件的路径和内容。例如:

// _archetypes/example.js

export default function () {
  return {
    path: "/pages/example.md",
    content: "Content of the file", // 文件的内容
  };
}

这个 archetype 将在你的 src 目录中创建文件 /pages/example.md,内容为 Content of the file。 archetype 文件名为 example.js,因此 archetype 的名称为 example。要执行它,只需运行 deno task lume new example (或者如果你正在使用 Lume CLI,则只需 lume new example)。

运行 archetypes

正如你所看到的,要运行一个 archetype,只需运行 deno task lume new [archetype_name]。 archetype 名称是文件名(不带扩展名),Lume 将在 src 文件夹内的 _archetypes 目录中搜索该文件。

可以使用相对路径运行其他 archetypes。在这种情况下,你需要包含文件的路径,包括扩展名。例如:

deno task lume new ./my-templates/new-post.ts

使用 URL 运行远程 archetype:

deno task lume new https://example.com/my-templates/new-post.ts

内容

content 变量可以是字符串、Uint8Array(用于二进制文件)或对象。如果内容是对象,它将根据路径的扩展名转换为字符串:

  • 如果 path 具有 ymlyaml 扩展名,则该对象将被字符串化为 YAML。
  • 如果 path 具有 json 扩展名,则该对象将被字符串化为 JSON。
  • 对于其他扩展名,该对象将转换为 frontmatter + 文本。

这是一个 YAML 转换的示例:

export default function () {
  return {
    path: "/pages/example.yml",
    content: {
      title: "Title content",
      content: "Page content",
    },
  };
}
title: Title content
content: Page content

相同的示例,但用于 JSON 转换:

export default function () {
  return {
    path: "/pages/example.json",
    content: {
      title: "Title content",
      content: "Page content",
    },
  };
}
{
  "title": "Title content",
  "content": "Page content"
}

相同的示例,但用于任何其他扩展名(例如,md):

export default function () {
  return {
    path: "/pages/example.md",
    content: {
      title: "Title content",
      content: "Page content",
    },
  };
}
---
title: Title content
---

Page content

传递参数

参数允许将变量传递给 archetype,以配置新内容的创建方式。例如,我们想要根据提供的标题创建新页面:

// _archetypes/page.ts

export default function (title: string) {
  const slug = title.replace(/\s+/g, "-").toLowerCase();

  return {
    path: `/pages/${slug}.md`,
    content: {
      title: title,
      content: "Page content",
    },
  };
}

此函数使用 title 参数来生成最终路径和内容。现在你可以运行 deno task lume new page "My first page" (或者如果你正在使用 Lume CLI,则 lume new page "My first page"),新的 /pages/my-first-page.md 文件将被创建。传递给 CLI 命令的任何额外参数都将传递给 archetype 的函数。

多个文件

可以从同一个 archetype 生成多个文件。为此,使用 generator 来 yield 所有文件。在以下示例中,archetype 在站点中创建一个新 section,其中包含多个页面和一个 _data.yml 文件:

// _archetypes/section.ts

export default function* (title: string) {
  const slug = title.replace(/\s+/g, "-").toLowerCase();

  // 创建共享数据
  yield {
    path: `/pages/${slug}/_data.yml`,
    content: {
      layout: "section.vto",
      section_title: title,
    },
  };

  // 创建另外 3 个页面
  const pages = [1, 2, 3];
  for (const page of pages) {
    yield {
      path: `/pages/${slug}/${page}.md`,
      content: {
        title: `Page ${page}`,
        content: "Write the content here", // 在这里写内容
      },
    };
  }
}

正如你所看到的,Lume 的 archetypes 既简单又灵活且功能强大。并且由于它们只是纯 JavaScript/TypeScript 文件,因此可以重用它们。例如,你可以创建一个 archetype,它可以导入其他 archetypes。