highlight.ts — astro Source File
Architecture documentation for highlight.ts, a typescript file in the astro codebase. 5 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 5feef60c_7aec_ab11_c4dc_ea28f338b6e8["highlight.ts"] f76e2597_e3e8_c502_d293_a666b44d49ce["hast"] 5feef60c_7aec_ab11_c4dc_ea28f338b6e8 --> f76e2597_e3e8_c502_d293_a666b44d49ce b2d95781_2713_6880_c3b6_efd677b54735["hast-util-from-html"] 5feef60c_7aec_ab11_c4dc_ea28f338b6e8 --> b2d95781_2713_6880_c3b6_efd677b54735 5d78fff7_3a36_1d89_f7d0_95b5a78053ed["hast-util-to-text"] 5feef60c_7aec_ab11_c4dc_ea28f338b6e8 --> 5d78fff7_3a36_1d89_f7d0_95b5a78053ed 5e13c883_40ff_3da5_f1c1_66b431659f2f["unist-util-remove-position"] 5feef60c_7aec_ab11_c4dc_ea28f338b6e8 --> 5e13c883_40ff_3da5_f1c1_66b431659f2f 0df90ade_50c4_95bc_61f9_dfbe85847bfd["unist-util-visit-parents"] 5feef60c_7aec_ab11_c4dc_ea28f338b6e8 --> 0df90ade_50c4_95bc_61f9_dfbe85847bfd style 5feef60c_7aec_ab11_c4dc_ea28f338b6e8 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import type { Element, Parent, Root } from 'hast';
import { fromHtml } from 'hast-util-from-html';
import { toText } from 'hast-util-to-text';
import { removePosition } from 'unist-util-remove-position';
import { visitParents } from 'unist-util-visit-parents';
type Highlighter = (
code: string,
language: string,
options?: { meta?: string },
) => Promise<Root | string>;
const languagePattern = /\blanguage-(\S+)\b/;
// Don’t highlight math code blocks by default.
export const defaultExcludeLanguages = ['math'];
/**
* A hast utility to syntax highlight code blocks with a given syntax highlighter.
*
* @param tree
* The hast tree in which to syntax highlight code blocks.
* @param highlighter
* A function which receives the code and language, and returns the HTML of a syntax
* highlighted `<pre>` element.
*/
export async function highlightCodeBlocks(
tree: Root,
highlighter: Highlighter,
excludeLanguages: string[] = [],
) {
const nodes: Array<{
node: Element;
language: string;
parent: Element;
grandParent: Parent;
}> = [];
// We’re looking for `<code>` elements
visitParents(tree, { type: 'element', tagName: 'code' }, (node, ancestors) => {
const parent = ancestors.at(-1);
// Whose parent is a `<pre>`.
if (parent?.type !== 'element' || parent.tagName !== 'pre') {
return;
}
// Where the `<code>` is the only child.
if (parent.children.length !== 1) {
return;
}
// And the `<code>` has a class name that starts with `language-`.
let languageMatch: RegExpMatchArray | null | undefined;
let { className } = node.properties;
if (typeof className === 'string') {
languageMatch = languagePattern.exec(className);
} else if (Array.isArray(className)) {
for (const cls of className) {
if (typeof cls !== 'string') {
continue;
}
languageMatch = languagePattern.exec(cls);
if (languageMatch) {
break;
}
}
}
const language = languageMatch?.[1] || 'plaintext';
if (excludeLanguages.includes(language) || defaultExcludeLanguages.includes(language)) {
return;
}
nodes.push({
node,
language,
parent,
grandParent: ancestors.at(-2)!,
});
});
for (const { node, language, grandParent, parent } of nodes) {
const meta = (node.data as any)?.meta ?? node.properties.metastring ?? undefined;
const code = toText(node, { whitespace: 'pre' });
const result = await highlighter(code, language, { meta });
let replacement: Element;
if (typeof result === 'string') {
// The replacement returns a root node with 1 child, the `<pre>` element replacement.
replacement = fromHtml(result, { fragment: true }).children[0] as Element;
// We just generated this node, so any positional information is invalid.
removePosition(replacement);
} else {
replacement = result.children[0] as Element;
}
// We replace the parent in its parent with the new `<pre>` element.
const index = grandParent.children.indexOf(parent);
grandParent.children[index] = replacement;
}
}
Domain
Subdomains
Functions
Types
Dependencies
- hast
- hast-util-from-html
- hast-util-to-text
- unist-util-remove-position
- unist-util-visit-parents
Source
Frequently Asked Questions
What does highlight.ts do?
highlight.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in highlight.ts?
highlight.ts defines 2 function(s): code, highlightCodeBlocks.
What does highlight.ts depend on?
highlight.ts imports 5 module(s): hast, hast-util-from-html, hast-util-to-text, unist-util-remove-position, unist-util-visit-parents.
Where is highlight.ts in the architecture?
highlight.ts is located at packages/markdown/remark/src/highlight.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/markdown/remark/src).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free