findCodeChild()
Recherche un élément enfant <code> à l'intérieur d'un élément parent.
import { findCodeChild } from "@unifast/core";Signature
function findCodeChild(element: HastElement): HastElement | undefinedParamètres
element
| Propriété | Type | Défaut | Description |
|---|---|---|---|
type | "element" | — | Identifiant du type de nœud |
tagName | string | — | Le nom de la balise HTML (typiquement "pre") |
properties | Record<string, unknown> | — | Propriétés de l’élément |
children | HastNode[] | — | Nœuds enfants à parcourir |
Valeur de retour
HastElement | undefined — Le premier élément enfant dont le tagName vaut "code", ou undefined si aucun enfant de ce type n’existe.
Utilisation
import { findCodeChild } from "@unifast/core";
import type { HastElement } from "@unifast/core";
const pre: HastElement = {
type: "element",
tagName: "pre",
properties: {},
children: [
{
type: "element",
tagName: "code",
properties: { className: ["language-js"] },
children: [{ type: "text", value: "const x = 1;" }],
},
],
};
const code = findCodeChild(pre);
console.log(code?.tagName);
// codeExemples
Trouver un élément de code dans un élément pre
import { findCodeChild, extractLang, extractText } from "@unifast/core";
import type { HastElement } from "@unifast/core";
const pre: HastElement = {
type: "element",
tagName: "pre",
properties: {},
children: [
{
type: "element",
tagName: "code",
properties: { className: ["language-rust"] },
children: [{ type: "text", value: 'fn main() { println!("hello"); }' }],
},
],
};
const code = findCodeChild(pre);
if (code) {
console.log(extractLang(code));
// rust
console.log(extractText(code));
// fn main() { println!("hello"); }
}Lorsqu’aucun enfant de code n’existe
import { findCodeChild } from "@unifast/core";
import type { HastElement } from "@unifast/core";
const pre: HastElement = {
type: "element",
tagName: "pre",
properties: {},
children: [
{ type: "text", value: "plain preformatted text" },
],
};
const code = findCodeChild(pre);
console.log(code);
// undefinedCombinaison avec visitHast pour la coloration syntaxique
import { visitHast, findCodeChild, extractLang } from "@unifast/core";
import type { HastNode, HastElement } from "@unifast/core";
const tree: HastNode = {
type: "root",
children: [
{
type: "element",
tagName: "pre",
properties: {},
children: [
{
type: "element",
tagName: "code",
properties: { className: ["language-js"] },
children: [{ type: "text", value: "const x = 1;" }],
},
],
},
],
};
visitHast(tree, (node) => {
if (node.type === "element" && node.tagName === "pre") {
const code = findCodeChild(node);
if (code) {
const lang = extractLang(code);
console.log(`Found code block with language: `);
// Found code block with language: js
}
}
});