extractText()
Trích xuất đệ quy toàn bộ nội dung văn bản từ một nút HAST.
import { extractText } from "@unifast/core";Chữ ký
function extractText(node: HastNode): stringTham số
node
| Thuộc tính | Kiểu | Mặc định | Mô tả |
|---|---|---|---|
type | string | — | Loại nút ("root", "element", "text", v.v.) |
children | HastNode[] | — | Các nút con (dành cho loại "root" và "element") |
value | string | — | Nội dung văn bản (dành cho loại "text") |
Giá trị trả về
string — Toàn bộ nội dung văn bản được nối lại từ nút hiện tại và các nút con cháu của nó.
Cách dùng
import { extractText } from "@unifast/core";
import type { HastElement } from "@unifast/core";
const element: HastElement = {
type: "element",
tagName: "p",
properties: {},
children: [
{ type: "text", value: "Hello " },
{
type: "element",
tagName: "strong",
properties: {},
children: [{ type: "text", value: "world" }],
},
],
};
const text = extractText(element);
console.log(text);
// Hello worldVí dụ
Trích xuất từ một phần tử đơn giản
import { extractText } from "@unifast/core";
import type { HastElement } from "@unifast/core";
const heading: HastElement = {
type: "element",
tagName: "h1",
properties: { id: "title" },
children: [{ type: "text", value: "Getting Started" }],
};
console.log(extractText(heading));
// Getting StartedTrích xuất từ các phần tử lồng nhau
import { extractText } from "@unifast/core";
import type { HastElement } from "@unifast/core";
const paragraph: HastElement = {
type: "element",
tagName: "p",
properties: {},
children: [
{ type: "text", value: "This is " },
{
type: "element",
tagName: "em",
properties: {},
children: [
{ type: "text", value: "deeply " },
{
type: "element",
tagName: "strong",
properties: {},
children: [{ type: "text", value: "nested" }],
},
],
},
{ type: "text", value: " content." },
],
};
console.log(extractText(paragraph));
// This is deeply nested content.Phần tử rỗng
import { extractText } from "@unifast/core";
import type { HastElement } from "@unifast/core";
const emptyDiv: HastElement = {
type: "element",
tagName: "div",
properties: {},
children: [],
};
console.log(extractText(emptyDiv));
// (empty string)Sinh slug cho tiêu đề
import { extractText } from "@unifast/core";
import type { HastElement } from "@unifast/core";
const heading: HastElement = {
type: "element",
tagName: "h2",
properties: {},
children: [
{ type: "text", value: "API " },
{
type: "element",
tagName: "code",
properties: {},
children: [{ type: "text", value: "Reference" }],
},
],
};
const slug = extractText(heading).toLowerCase().replace(/\s+/g, "-");
console.log(slug);
// api-reference