extractLang()
Extrai a linguagem de programação do className de um elemento de código.
import { extractLang } from "@unifast/core";Assinatura
function extractLang(code: HastElement): string | nullParâmetros
code
| Propriedade | Tipo | Padrão | Descrição |
|---|---|---|---|
type | "element" | — | Identificador do tipo de nó |
tagName | string | — | O nome da tag HTML (tipicamente "code") |
properties | Record<string, unknown> | — | Propriedades do elemento, incluindo className |
children | HastNode[] | — | Nós filhos do elemento |
Retorna
string | null — O identificador da linguagem extraído da primeira classe language-*, ou null se nenhuma classe de linguagem for encontrada.
Uso
import { extractLang } from "@unifast/core";
import type { HastElement } from "@unifast/core";
const codeElement: HastElement = {
type: "element",
tagName: "code",
properties: { className: ["language-typescript"] },
children: [{ type: "text", value: "const x = 1;" }],
};
const lang = extractLang(codeElement);
console.log(lang);
// typescriptExemplos
Extrair linguagem de um elemento de código
import { extractLang } from "@unifast/core";
import type { HastElement } from "@unifast/core";
const code: HastElement = {
type: "element",
tagName: "code",
properties: { className: ["language-js", "highlight"] },
children: [{ type: "text", value: "console.log('hello');" }],
};
console.log(extractLang(code));
// jsQuando não existe classe de linguagem
import { extractLang } from "@unifast/core";
import type { HastElement } from "@unifast/core";
const code: HastElement = {
type: "element",
tagName: "code",
properties: { className: ["highlight"] },
children: [{ type: "text", value: "plain text" }],
};
console.log(extractLang(code));
// nullQuando className não é um array
import { extractLang } from "@unifast/core";
import type { HastElement } from "@unifast/core";
const code: HastElement = {
type: "element",
tagName: "code",
properties: {},
children: [{ type: "text", value: "no classes" }],
};
console.log(extractLang(code));
// nullUsando com findCodeChild
import { extractLang, 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-python"] },
children: [{ type: "text", value: "print('hello')" }],
},
],
};
const code = findCodeChild(pre);
if (code) {
console.log(extractLang(code));
// python
}