excerpt()

從文件內容中擷取摘要。

import { excerpt } from "@unifast/node";

函式簽名

function excerpt(options?: ExcerptPluginOptions): UnifastPlugin

參數

options?

摘要擷取的設定

屬性型別預設值說明
separatorstring"<!-- more -->"用來區隔摘要與其餘內容的註解標記
fallbackParagraphsnumber1找不到分隔標記時,要做為摘要的開頭段落數
fallbackCharactersnumberundefined備援摘要的最大字元長度(會在詞界處截斷)

同時設定 fallbackParagraphsfallbackCharacters 時,fallbackParagraphs 優先生效。

回傳值

此外掛會在 compile 結果中加入 excerpt 屬性:

屬性型別說明
result.excerptstring | undefined從文件中擷取出的純文字摘要

用法

import { compile, excerpt } from "@unifast/node";

const md = `
This is the introduction to my blog post.

<!-- more -->

The rest of the article continues here with more details.
`;

const result = compile(md, {
  plugins: [excerpt()],
});

console.log(result.excerpt);
// "This is the introduction to my blog post."

範例

搭配分隔標記

在 Markdown 中放置 <!-- more --> 註解,即可明確標示摘要的結束位置:

import { compile, excerpt } from "@unifast/node";

const md = `
# My Blog Post

This is a **bold** introduction with some content.

Here is a second paragraph still in the excerpt.

<!-- more -->

This content is not included in the excerpt.
`;

const result = compile(md, {
  plugins: [excerpt()],
});

console.log(result.excerpt);
// "My Blog Post This is a bold introduction with some content. Here is a second paragraph still in the excerpt."

備援為前幾段

當找不到分隔標記時,外掛會退回擷取前 N 個段落:

import { compile, excerpt } from "@unifast/node";

const md = `
# My Blog Post

This is the first paragraph of my article.

This is the second paragraph with more details.

This is the third paragraph.
`;

const result = compile(md, {
  plugins: [
    excerpt({
      fallbackParagraphs: 2,
    }),
  ],
});

console.log(result.excerpt);
// "This is the first paragraph of my article. This is the second paragraph with more details."

備援為字元上限

將摘要截斷至指定的最大字元長度,並在詞界處斷開:

import { compile, excerpt } from "@unifast/node";

const md = `
This is a long article that goes on and on with lots of content.
`;

const result = compile(md, {
  plugins: [
    excerpt({
      fallbackCharacters: 30,
    }),
  ],
});

console.log(result.excerpt);
// "This is a long article that"