MCPcopy
hub / github.com/marktext/marktext / htmlTag

Function htmlTag

packages/muya/src/inlineRenderer/renderer/htmlTag.ts:8–149  ·  view source on GitHub ↗
(
    this: Renderer,
    {
        h,
        cursor,
        block,
        token,
        outerClass,
    }: ISyntaxRenderOptions & { token: HTMLTagToken },
)

Source from the content-addressed store, hash-verified

6import sanitize, { isValidAttribute } from '../../utils/dompurify';
7
8export default function htmlTag(
9 this: Renderer,
10 {
11 h,
12 cursor,
13 block,
14 token,
15 outerClass,
16 }: ISyntaxRenderOptions & { token: HTMLTagToken },
17) {
18 const { tag, openTag, closeTag, children, attrs } = token;
19
20 const className = children?.length
21 ? this.getClassName(outerClass, block, token, cursor)
22 : CLASS_NAMES.MU_GRAY;
23 const tagClassName
24 = className === CLASS_NAMES.MU_HIDE ? className : CLASS_NAMES.MU_HTML_TAG;
25 const { start, end } = token.range;
26 const openContent = this.highlight(
27 h,
28 block,
29 start,
30 start + openTag.length,
31 token,
32 );
33 const closeContent = closeTag
34 ? this.highlight(h, block, end - closeTag.length, end, token)
35 : '';
36
37 const anchor
38 = Array.isArray(children) && children.length && tag !== 'ruby' // important
39 ? children.reduce((acc: VNode[], to: Token) => {
40 // The original passed a `className` field here too, but
41 // receivers read `outerClass` — so the field was
42 // silently dropped under the previous `as any` cast.
43 // Preserve that runtime behavior: don't propagate it.
44 const chunk = this.dispatch(snakeToCamel(to.type), {
45 h,
46 cursor,
47 block,
48 token: to,
49 });
50
51 return Array.isArray(chunk) ? [...acc, ...chunk] : [...acc, chunk];
52 }, [])
53 : '';
54
55 switch (tag) {
56 // Handle html img.
57 case 'img': {
58 // `<img>` html_tag tokens and markdown image tokens overlap on the
59 // fields `image()` actually reads (`raw`, `range`, `attrs.src/alt/
60 // title/width/height/'data-align'`), but their static shapes
61 // diverge — HTMLTagToken.attrs is `Record<string, string | null>`,
62 // ImageToken.attrs is the strict `{ src; title; alt }`. Routing
63 // html `<img>` through `image()` is a deliberate runtime contract;
64 // express the structural pun in one place.
65 // eslint-disable-next-line no-restricted-syntax

Callers

nothing calls this directly

Calls 7

snakeToCamelFunction · 0.90
hFunction · 0.85
highlightMethod · 0.80
sanitizeFunction · 0.50
getClassNameMethod · 0.45
reduceMethod · 0.45
dispatchMethod · 0.45

Tested by

no test coverage detected