html-token-transform.ts — astro Source File
Architecture documentation for html-token-transform.ts, a typescript file in the astro codebase. 3 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR b514e53d_6f58_26e2_b444_6fdf850f8be2["html-token-transform.ts"] 9e63cbe8_497c_4ffa_fe72_98f051e32a00["markdoc"] b514e53d_6f58_26e2_b444_6fdf850f8be2 --> 9e63cbe8_497c_4ffa_fe72_98f051e32a00 9e21c551_ca18_5809_5a40_2a76c92cd970["htmlparser2"] b514e53d_6f58_26e2_b444_6fdf850f8be2 --> 9e21c551_ca18_5809_5a40_2a76c92cd970 d0e63031_fa77_ad19_cd9f_40b120aed07d["token"] b514e53d_6f58_26e2_b444_6fdf850f8be2 --> d0e63031_fa77_ad19_cd9f_40b120aed07d style b514e53d_6f58_26e2_b444_6fdf850f8be2 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
/** biome-ignore-all lint/correctness/noUnusedImports: not correctly detected because type isn't exported */
import type { Tokenizer } from '@markdoc/markdoc';
import { Parser } from 'htmlparser2';
// @ts-expect-error This type isn't exported
import type * as Token from 'markdown-it/lib/token';
export function htmlTokenTransform(tokenizer: Tokenizer, tokens: Token[]): Token[] {
const output: Token[] = [];
// hold a lazy buffer of text and process it only when necessary
let textBuffer = '';
let inCDATA = false;
const appendText = (text: string) => {
textBuffer += text;
};
// process the current text buffer w/ Markdoc's Tokenizer for tokens
const processTextBuffer = () => {
if (textBuffer.length > 0) {
// tokenize the text buffer to look for structural markup tokens
const toks = tokenizer.tokenize(textBuffer);
// when we tokenize some raw text content, it's basically treated like Markdown, and will result in a paragraph wrapper, which we don't want
// in this scenario, we just want to generate a text token, but, we have to tokenize it in case there's other structural markup
if (toks.length === 3) {
const first = toks[0];
const second = toks[1];
const third: Token | undefined = toks.at(2);
if (
first.type === 'paragraph_open' &&
second.type === 'inline' &&
third &&
third.type === 'paragraph_close' &&
Array.isArray(second.children)
) {
for (const tok of second.children as Token[]) {
// if the given token is a 'text' token and its trimmed content is the same as the pre-tokenized text buffer, use the original
// text buffer instead to preserve leading/trailing whitespace that is lost during tokenization of pure text content
if (tok.type === 'text') {
if (tok.content.trim() == textBuffer.trim()) {
tok.content = textBuffer;
}
}
output.push(tok);
}
} else {
// some other markup that happened to be 3 tokens, push tokens as-is
for (const tok of toks) {
output.push(tok);
}
}
} else {
// some other tokenized markup, push tokens as-is
for (const tok of toks) {
output.push(tok);
}
// ... (191 more lines)
Domain
Subdomains
Functions
Dependencies
- htmlparser2
- markdoc
- token
Source
Frequently Asked Questions
What does html-token-transform.ts do?
html-token-transform.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 html-token-transform.ts?
html-token-transform.ts defines 9 function(s): findExtraParagraphUnderHtml, htmlTokenTransform, isExtraParagraphPatternMatch, isHtmlTagClose, isHtmlTagOpen, isInline, isParagraphClose, isParagraphOpen, mutateAndCollapseExtraParagraphsUnderHtml.
What does html-token-transform.ts depend on?
html-token-transform.ts imports 3 module(s): htmlparser2, markdoc, token.
Where is html-token-transform.ts in the architecture?
html-token-transform.ts is located at packages/integrations/markdoc/src/html/transform/html-token-transform.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/integrations/markdoc/src/html/transform).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free