Files
paseo/packages/react-native-enriched-markdown/lib/module/web/styles.js
T
Mohamed Boudra 80c44321cc Move the Markdown engine into the repository and fix two Android list layouts (#10499)
* chore(app): vendor react-native-enriched-markdown with the Paseo patch applied

Copy react-native-enriched-markdown@1.2.0-nightly-20261014-766fc0e67 (upstream
software-mansion/enriched-markdown 766fc0e67) into
packages/react-native-enriched-markdown, with
patches/react-native-enriched-markdown+1.2.0-nightly-20261014-766fc0e67.patch
applied. Nothing else changes in this commit; the package is not a workspace yet.

Reproduce:

  npm pack react-native-enriched-markdown@1.2.0-nightly-20261014-766fc0e67
  tar xzf react-native-enriched-markdown-*.tgz
  mkdir -p t/node_modules && mv package t/node_modules/react-native-enriched-markdown
  (cd t && git init -q && git apply ../patches/react-native-enriched-markdown+*.patch)
  diff -r t/node_modules/react-native-enriched-markdown packages/react-native-enriched-markdown

The diff is empty.

* chore(app): consume react-native-enriched-markdown from its workspace

Make packages/react-native-enriched-markdown a private workspace and point the
app at it, then remove the patch-package patch it replaces.

- package.json: private, version follows the root like every workspace, only the
  postinstall script that downloads tree-sitter grammars. Upstream's
  devDependencies would install a second react-native, so they are gone.
- The package keeps its name, so autolinking, the Gradle project
  :react-native-enriched-markdown, the CocoaPods spec and imports are unchanged.
- Lockfile: drop the nested registry copy under packages/app/node_modules; npm
  links node_modules/react-native-enriched-markdown to the workspace.
- Formatting, lint and knip skip the vendored code. CI routes changes to it
  through the app and browser suites.
- The codegen test checks the vendored package's generated props.
- docs/markdown-renderer.md describes the fork and how to edit it.

* fix(app): keep numbered sub-list markers off their text on Android

From #9964, moved from the package patch into the vendored package.

On Android, a numbered list nested in a bulleted item drew "1." over
the first letters of each item. A nested item's text indent summed the
root list's narrow bullet column with a fixed marginLeft, while its
marker was right-aligned to its own wider "99." column, so the number
landed on the text. iOS already indents from the item's own column.

Backport the Android hunks of upstream enriched-markdown #1015 (size
the number column to the widest marker) and #1023 (nested items start
their text from their own marker column).

test:android now also runs the backported ListRenderingTest and
TaskListInteractionTest suites. docs/markdown-renderer.md lists the
fork's backports, this one and #924, which the removed postinstall
comment used to track.

The vendored tree equals the #9964 patch applied to the pristine
package.

* fix(app): keep Android assistant replies inside their turns

From #10380, moved from the package patch into the vendored package.

On Android, an assistant reply with blockquotes nested inside a list
could overlap its "Worked for ..." footer and the next user message:
the displayed text and the layout measurement applied spans in
different orders, and the list-wide line height inflated the blockquote
spacers when it ran after them.

Use the same span order for Android Markdown measurement and display.
Apply list line height before blockquote spacers so nested quotes keep
their configured padding and the reply fits above the turn footer.

test:android now also runs ListBlockquoteMeasurementTest.

Applied on top of #9964; the two changes share only ListRenderer.kt,
where #10380's import and line-height call sit beside #9964's marker
width code. Every other file equals the #10380 patch applied to the
pristine package.

* chore(app): match the vendored Markdown web block renderer source to its build

The package patch changed lib/module/web/renderers/BlockRenderers.js (the
renderers.codeBlock override and the zoom-in cursor on images) but not
src/web/renderers/BlockRenderers.tsx. The app runs lib, so behavior does
not change. Port both changes into src so src stays the source of lib.
2026-11-23 10:15:43 +07:00

556 lines
16 KiB
JavaScript

"use strict";
const normalizeFontFamily = value => value || undefined;
const VALID_FONT_WEIGHTS = new Set(['normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900']);
const normalizeFontWeight = value => {
if (!value) return undefined;
if (VALID_FONT_WEIGHTS.has(value)) return value;
return undefined;
};
const normalizeTextAlign = value => value === 'auto' ? undefined : value;
// 'default' is an AST sentinel for "unspecified column alignment".
function resolveColumnAlign(align) {
if (align === 'center' || align === 'right') return align;
return 'left';
}
export function zeroTrailingMargins(style) {
return {
...style,
paragraph: {
...style.paragraph,
marginBottom: 0
},
h1: {
...style.h1,
marginBottom: 0
},
h2: {
...style.h2,
marginBottom: 0
},
h3: {
...style.h3,
marginBottom: 0
},
h4: {
...style.h4,
marginBottom: 0
},
h5: {
...style.h5,
marginBottom: 0
},
h6: {
...style.h6,
marginBottom: 0
},
blockquote: {
...style.blockquote,
marginBottom: 0
},
list: {
...style.list,
marginBottom: 0
},
codeBlock: {
...style.codeBlock,
marginBottom: 0
},
thematicBreak: {
...style.thematicBreak,
marginBottom: 0
},
image: {
...style.image,
marginBottom: 0
},
video: {
...style.video,
marginBottom: 0
},
math: {
...style.math,
marginBottom: 0
},
table: {
...style.table,
marginBottom: 0
}
};
}
export function toHeadingLevel(level) {
const clamped = Math.max(1, Math.min(6, parseInt(level, 10) || 1));
return `h${clamped}`;
}
function baseBlock(block) {
return {
fontSize: block.fontSize,
fontFamily: normalizeFontFamily(block.fontFamily),
fontWeight: normalizeFontWeight(block.fontWeight),
color: block.color,
lineHeight: `${block.lineHeight}px`,
marginTop: block.marginTop,
marginBottom: block.marginBottom,
textAlign: normalizeTextAlign(block.textAlign)
};
}
function paragraphStyle(style) {
return baseBlock(style.paragraph);
}
function paragraphInBlockquoteStyle(style) {
// Keep the paragraph's block margins so spacing between blocks inside a quote
// matches the top-level document (e.g. the gap before a heading). The last
// child's trailing margin is zeroed by a global blockquote :last-child rule
// (see globalStyles), mirroring native's trailing-margin trim - the quote is a
// flex-item BFC, so that margin would otherwise not collapse out of the box.
return baseBlock(style.paragraph);
}
function headingStyle(style, level) {
return baseBlock(style[toHeadingLevel(level)]);
}
function blockquoteStyle(style) {
const blockquote = style.blockquote;
return {
fontSize: blockquote.fontSize,
fontFamily: normalizeFontFamily(blockquote.fontFamily),
fontWeight: normalizeFontWeight(blockquote.fontWeight),
color: blockquote.color,
lineHeight: `${blockquote.lineHeight}px`,
marginTop: blockquote.marginTop,
marginBottom: blockquote.marginBottom,
marginInlineStart: 0,
// reset UA default (40px in LTR, auto in RTL)
marginInlineEnd: 0,
paddingInlineStart: blockquote.gapWidth + blockquote.padding,
paddingInlineEnd: blockquote.padding,
paddingTop: blockquote.padding,
paddingBottom: blockquote.padding,
borderInlineStart: `${blockquote.borderWidth}px solid ${blockquote.borderColor}`,
borderRadius: blockquote.borderRadius,
backgroundColor: blockquote.backgroundColor
};
}
function listStyle(style, isTaskList = false) {
const list = style.list;
return {
listStylePosition: 'outside',
fontSize: list.fontSize,
fontFamily: normalizeFontFamily(list.fontFamily),
fontWeight: normalizeFontWeight(list.fontWeight),
color: list.color,
lineHeight: `${list.lineHeight}px`,
marginTop: list.marginTop,
marginBottom: list.marginBottom,
paddingInlineStart: isTaskList ? 0 : list.marginLeft
};
}
function codeBlockStyle(style) {
const codeBlock = style.codeBlock;
return {
fontSize: codeBlock.fontSize,
fontFamily: normalizeFontFamily(codeBlock.fontFamily),
fontWeight: normalizeFontWeight(codeBlock.fontWeight),
color: codeBlock.color,
lineHeight: `${codeBlock.lineHeight}px`,
backgroundColor: codeBlock.backgroundColor,
border: `${codeBlock.borderWidth}px solid ${codeBlock.borderColor}`,
borderRadius: codeBlock.borderRadius,
padding: codeBlock.padding,
margin: 0,
marginTop: codeBlock.marginTop,
marginBottom: codeBlock.marginBottom,
overflowX: 'auto',
direction: 'ltr'
};
}
function thematicBreakStyle(style) {
const thematicBreak = style.thematicBreak;
return {
border: 'none',
// reset UA borders on all sides before drawing only the top
borderTop: `${thematicBreak.height}px solid ${thematicBreak.color}`,
marginTop: thematicBreak.marginTop,
marginBottom: thematicBreak.marginBottom,
width: '100%' // <hr> as a flex item doesn't auto-stretch — must be explicit
};
}
const RESIZE_MODE_TO_OBJECT_FIT = {
contain: 'contain',
cover: 'cover',
stretch: 'fill',
center: 'scale-down',
none: 'none'
};
function imageStyle(style) {
const image = style.image;
const base = {
borderRadius: image.borderRadius,
marginTop: image.marginTop,
marginBottom: image.marginBottom,
maxWidth: '100%',
display: 'block'
};
// Sizing precedence: aspectRatio > maxHeight > height. resizeMode '' means
// legacy sizing — emit today's exact CSS for backward compatibility.
if (image.resizeMode === '') {
return {
...base,
height: image.height
};
}
const objectFit = RESIZE_MODE_TO_OBJECT_FIT[image.resizeMode] ?? 'cover';
if (image.aspectRatio > 0) {
return {
...base,
width: '100%',
aspectRatio: image.aspectRatio,
objectFit
};
}
if (image.maxHeight > 0) {
return {
...base,
width: '100%',
height: 'auto',
maxHeight: image.maxHeight,
objectFit
};
}
return {
...base,
width: '100%',
height: image.height,
objectFit
};
}
function videoStyle(style) {
const video = style.video;
const base = {
width: '100%',
maxWidth: '100%',
display: 'block',
borderRadius: video.borderRadius,
backgroundColor: video.backgroundColor,
marginTop: video.marginTop,
marginBottom: video.marginBottom
};
if (video.aspectRatio > 0) {
base.aspectRatio = video.aspectRatio;
}
return base;
}
function inlineImageStyle(style) {
const size = style.inlineImage.size;
// Inline images match the line height and keep their aspect ratio, so wide
// badges stay legible instead of shrinking into a square.
return {
width: 'auto',
height: size,
maxWidth: '100%',
verticalAlign: 'middle',
display: 'inline'
};
}
function strongStyle(style) {
const strong = style.strong;
return {
fontFamily: normalizeFontFamily(strong.fontFamily),
fontWeight: normalizeFontWeight(strong.fontWeight) || 'bold',
color: strong.color ?? style.paragraph.color
};
}
function emphasisStyle(style) {
const emphasis = style.em;
return {
fontFamily: normalizeFontFamily(emphasis.fontFamily),
fontStyle: emphasis.fontStyle || 'italic',
// '' means "inherit default" → fall back to italic
color: emphasis.color ?? style.paragraph.color
};
}
function codeStyle(style) {
const code = style.code;
return {
fontFamily: normalizeFontFamily(code.fontFamily),
fontSize: code.fontSize || `${code.fontScale}em`,
color: code.color,
backgroundColor: code.backgroundColor,
border: `1px solid ${code.borderColor}`,
borderRadius: 3,
padding: '1px 4px',
direction: 'ltr',
unicodeBidi: 'embed'
};
}
function linkStyle(style) {
const link = style.link;
return {
color: link.color,
fontFamily: normalizeFontFamily(link.fontFamily),
textDecoration: link.underline ? 'underline' : 'none'
};
}
/** Compiled variant regexes keyed by the normalized style object. Avoids recompiling on every link render. */
const _variantRegexCache = new WeakMap();
function getCompiledVariantRegexes(variants) {
let compiled = _variantRegexCache.get(variants);
if (!compiled) {
compiled = variants.map(variant => {
try {
return new RegExp(variant.pattern);
} catch {
return null;
}
});
_variantRegexCache.set(variants, compiled);
}
return compiled;
}
export function linkStyleForUrl(style, url) {
const base = style.link;
const compiledVariantRegexes = getCompiledVariantRegexes(style.linkVariants);
const variantIndex = compiledVariantRegexes.findIndex(regex => regex !== null && regex.test(url));
const resolved = variantIndex !== -1 ? style.linkVariants[variantIndex] : base;
const backgroundColor = resolved.backgroundColor;
return {
color: resolved.color,
fontFamily: normalizeFontFamily(resolved.fontFamily),
textDecoration: resolved.underline ? 'underline' : 'none',
...(backgroundColor && backgroundColor !== 'transparent' ? {
backgroundColor
} : undefined)
};
}
function strikethroughStyle(style) {
return {
textDecorationLine: 'line-through',
textDecorationColor: style.strikethrough.color
};
}
function underlineStyle(style) {
return {
textDecorationLine: 'underline',
textDecorationColor: style.underline.color
};
}
function superscriptStyle(style) {
const {
fontScale,
baselineOffsetScale
} = style.superscript;
return {
fontSize: `${fontScale}em`,
verticalAlign: `${baselineOffsetScale}em`,
lineHeight: 0
};
}
function subscriptStyle(style) {
const {
fontScale,
baselineOffsetScale
} = style.subscript;
return {
fontSize: `${fontScale}em`,
verticalAlign: `-${baselineOffsetScale}em`,
lineHeight: 0
};
}
function highlightStyle(style) {
return {
backgroundColor: style.highlight.backgroundColor,
color: style.highlight.color !== style.paragraph.color ? style.highlight.color : 'inherit'
};
}
function mathInlineStyle(style) {
return {
color: style.inlineMath.color
};
}
function mathDisplayStyle(style) {
const math = style.math;
return {
fontSize: math.fontSize,
color: math.color,
backgroundColor: math.backgroundColor,
padding: math.padding,
marginTop: math.marginTop,
marginBottom: math.marginBottom,
textAlign: normalizeTextAlign(math.textAlign),
overflowX: 'auto'
};
}
function tableStyle(style) {
const table = style.table;
return {
borderCollapse: 'collapse',
width: '100%',
fontSize: table.fontSize,
fontFamily: normalizeFontFamily(table.fontFamily),
fontWeight: normalizeFontWeight(table.fontWeight),
color: table.color,
lineHeight: `${table.lineHeight}px`,
border: `${table.borderWidth}px solid ${table.borderColor}`
};
}
export function listItemStyle(style, isTask, isFirstChild) {
const {
itemSpacing
} = style.list;
const marginTop = !isFirstChild && itemSpacing > 0 ? itemSpacing : undefined;
if (isTask) {
return {
listStyle: 'none',
marginTop
};
}
return marginTop !== undefined ? {
marginTop
} : undefined;
}
export function checkedTaskTextStyle(style) {
const taskList = style.taskList;
return {
color: taskList.checkedTextColor || undefined,
textDecorationLine: taskList.checkedStrikethrough ? 'line-through' : undefined
};
}
/**
* Checkbox style used when `enableTaskListItemToggle` is `false`. Pointer-inert
* so the browser paints no hover/active state on a checkbox that cannot be
* toggled; appearance is otherwise identical to the enabled checkbox.
*/
function taskCheckboxDisabledStyle(style) {
return {
...taskCheckboxStyle(style),
pointerEvents: 'none',
cursor: 'default'
};
}
function taskCheckboxStyle(style) {
const taskList = style.taskList;
return {
width: taskList.checkboxSize,
height: taskList.checkboxSize,
borderRadius: taskList.checkboxBorderRadius,
marginInlineEnd: 6,
accentColor: taskList.checkedColor,
verticalAlign: 'middle'
};
}
export function tableBodyRowStyle(style, rowIndex) {
const table = style.table;
return {
backgroundColor: rowIndex % 2 === 0 ? table.rowEvenBackgroundColor : table.rowOddBackgroundColor
};
}
function tableWrapperStyle(style) {
const table = style.table;
const alignment = table.align ? {
width: 'fit-content',
maxWidth: '100%',
marginLeft: table.align === 'left' ? 0 : 'auto',
marginRight: table.align === 'right' ? 0 : 'auto'
} : {};
return {
overflowX: 'auto',
overflowY: 'hidden',
marginTop: table.marginTop,
marginBottom: table.marginBottom,
...alignment,
// borderRadius must live on the wrapper, not the <table> — border-collapse:
// collapse causes browsers to ignore border-radius on the table element itself.
borderRadius: table.borderRadius
};
}
function tableHeaderCellStyle(style, align) {
const table = style.table;
return {
backgroundColor: table.headerBackgroundColor,
color: table.headerTextColor,
fontFamily: normalizeFontFamily(table.headerFontFamily) ?? normalizeFontFamily(table.fontFamily),
fontWeight: 'bold',
padding: `${table.cellPaddingVertical}px ${table.cellPaddingHorizontal}px`,
border: `${table.borderWidth}px solid ${table.borderColor}`,
textAlign: resolveColumnAlign(align)
};
}
function tableCellStyle(style, align) {
const table = style.table;
return {
padding: `${table.cellPaddingVertical}px ${table.cellPaddingHorizontal}px`,
border: `${table.borderWidth}px solid ${table.borderColor}`,
textAlign: resolveColumnAlign(align)
};
}
export const parseErrorFallbackStyle = {
whiteSpace: 'pre-wrap',
margin: 0
};
const stylesStore = new WeakMap();
export function buildStyles(style) {
const cached = stylesStore.get(style);
if (cached) return cached;
const codeBlock = codeBlockStyle(style);
const result = {
paragraph: paragraphStyle(style),
paragraphInBlockquote: paragraphInBlockquoteStyle(style),
h1: headingStyle(style, '1'),
h2: headingStyle(style, '2'),
h3: headingStyle(style, '3'),
h4: headingStyle(style, '4'),
h5: headingStyle(style, '5'),
h6: headingStyle(style, '6'),
blockquote: blockquoteStyle(style),
list: listStyle(style),
listNested: {
...listStyle(style),
// A nested list sits directly under its parent item's text; itemSpacing
// (not the list's outer margins) controls that gap, like on native.
marginTop: Math.max(0, style.list.itemSpacing),
marginBottom: 0
},
listTask: listStyle(style, true),
codeBlock,
codeBlockFont: {
fontFamily: codeBlock.fontFamily
},
thematicBreak: thematicBreakStyle(style),
image: imageStyle(style),
inlineImage: inlineImageStyle(style),
strong: strongStyle(style),
emphasis: emphasisStyle(style),
code: codeStyle(style),
link: linkStyle(style),
strikethrough: strikethroughStyle(style),
underline: underlineStyle(style),
superscript: superscriptStyle(style),
subscript: subscriptStyle(style),
highlight: highlightStyle(style),
mathInline: mathInlineStyle(style),
mathDisplay: mathDisplayStyle(style),
table: tableStyle(style),
tableWrapper: tableWrapperStyle(style),
tableHeaderCell: {
left: tableHeaderCellStyle(style, 'left'),
center: tableHeaderCellStyle(style, 'center'),
right: tableHeaderCellStyle(style, 'right'),
default: tableHeaderCellStyle(style, 'default')
},
tableCell: {
left: tableCellStyle(style, 'left'),
center: tableCellStyle(style, 'center'),
right: tableCellStyle(style, 'right'),
default: tableCellStyle(style, 'default')
},
taskCheckbox: taskCheckboxStyle(style),
taskCheckboxDisabled: taskCheckboxDisabledStyle(style),
video: videoStyle(style)
};
stylesStore.set(style, result);
return result;
}
//# sourceMappingURL=styles.js.map