mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-26 02:05:31 +00:00
* 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.
311 lines
12 KiB
JavaScript
311 lines
12 KiB
JavaScript
#!/usr/bin/env node
|
|
// Thin wrapper that invokes vendor-grammars.mjs and vendor-ratex.mjs in
|
|
// consumer mode (--from-npm) to download vendored native assets after install.
|
|
//
|
|
// In the monorepo, grammar-versions.json is not present at the expected path
|
|
// (it is only copied during prepack), so this script is a no-op -- the
|
|
// `prepare` script handles vendoring via the same scripts with monorepo paths.
|
|
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { spawnSync } from 'node:child_process';
|
|
|
|
const PKG_ROOT = path.dirname(fileURLToPath(import.meta.url));
|
|
const LOG = '[react-native-enriched-markdown]';
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Consumer opt-out lookup. Kept in this file on purpose: downstream forks pack
|
|
// the postinstall by name, so it must not import siblings.
|
|
//
|
|
// npm/yarn/pnpm set INIT_CWD to the directory the install ran from. For a plain app
|
|
// that is the app itself. In a monorepo it is the workspace root, which usually has
|
|
// no `enriched-markdown` block of its own, so the lookup continues into the
|
|
// workspace members that depend on this package. A root block, when present, wins.
|
|
// Workspaces share one node_modules, so an asset is skipped only when every
|
|
// consuming app opts out of it.
|
|
|
|
const PACKAGE_NAME = 'react-native-enriched-markdown';
|
|
const MAX_WALK_DEPTH = 6;
|
|
|
|
function readJson(file) {
|
|
try {
|
|
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function isDir(dir) {
|
|
try {
|
|
return fs.statSync(dir).isDirectory();
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function configOf(pkg) {
|
|
const config = pkg?.['enriched-markdown'];
|
|
return config && typeof config === 'object' && !Array.isArray(config) ? config : null;
|
|
}
|
|
|
|
/** True for any `*ependencies` map (dependencies, devDependencies, peerDependencies, ...). */
|
|
function dependsOnPackage(pkg) {
|
|
return Object.entries(pkg ?? {}).some(
|
|
([field, value]) =>
|
|
/[dD]ependencies$/.test(field) &&
|
|
value && typeof value === 'object' && !Array.isArray(value) &&
|
|
Object.hasOwn(value, PACKAGE_NAME)
|
|
);
|
|
}
|
|
|
|
function subdirs(dir) {
|
|
let entries;
|
|
try {
|
|
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
} catch {
|
|
return [];
|
|
}
|
|
return entries
|
|
.filter((e) => e.isDirectory() && e.name !== 'node_modules' && !e.name.startsWith('.'))
|
|
.map((e) => path.join(dir, e.name));
|
|
}
|
|
|
|
function walk(dir, depth = 0) {
|
|
if (depth >= MAX_WALK_DEPTH) return [dir];
|
|
return [dir, ...subdirs(dir).flatMap((child) => walk(child, depth + 1))];
|
|
}
|
|
|
|
function segmentToRegExp(segment) {
|
|
const escaped = segment.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '[^/]*');
|
|
return new RegExp(`^${escaped}$`);
|
|
}
|
|
|
|
/** Directories matched by one workspace pattern such as `apps/*` or `packages/**`. */
|
|
function expand(root, pattern) {
|
|
const segments = pattern.split('/').filter((s) => s && s !== '.');
|
|
let dirs = [root];
|
|
for (const segment of segments) {
|
|
const next = [];
|
|
for (const dir of dirs) {
|
|
if (segment === '**') {
|
|
next.push(...walk(dir));
|
|
} else if (segment.includes('*')) {
|
|
const re = segmentToRegExp(segment);
|
|
next.push(...subdirs(dir).filter((d) => re.test(path.basename(d))));
|
|
} else {
|
|
next.push(path.join(dir, segment));
|
|
}
|
|
}
|
|
dirs = next;
|
|
}
|
|
return dirs.filter(isDir);
|
|
}
|
|
|
|
/** Strips a trailing `# comment` and surrounding quotes from one YAML scalar. */
|
|
function yamlScalar(text) {
|
|
return text.replace(/\s+#.*$/, '').trim().replace(/^(['"])(.*)\1$/, '$2');
|
|
}
|
|
|
|
/**
|
|
* The `packages:` list of pnpm-workspace.yaml, read without a YAML parser. Covers
|
|
* the block form (`- apps/*`) and the one-line flow form (`packages: ["apps/*"]`),
|
|
* which is all pnpm's own docs use.
|
|
*/
|
|
function readPnpmWorkspacePatterns(root) {
|
|
let text;
|
|
try {
|
|
text = fs.readFileSync(path.join(root, 'pnpm-workspace.yaml'), 'utf8');
|
|
} catch {
|
|
return [];
|
|
}
|
|
const patterns = [];
|
|
let inPackages = false;
|
|
for (const line of text.split(/\r?\n/)) {
|
|
const key = line.match(/^packages\s*:(.*)$/);
|
|
if (key) {
|
|
const flow = yamlScalar(key[1]).match(/^\[(.*)\]$/);
|
|
if (flow) {
|
|
return flow[1].split(',').map(yamlScalar).filter(Boolean);
|
|
}
|
|
inPackages = true;
|
|
continue;
|
|
}
|
|
if (!inPackages) continue;
|
|
if (/^\s*(#|$)/.test(line)) continue;
|
|
const item = line.match(/^\s+-\s*(.+)$/);
|
|
if (item) {
|
|
const pattern = yamlScalar(item[1]);
|
|
if (pattern) patterns.push(pattern);
|
|
} else if (/^\S/.test(line)) {
|
|
break;
|
|
}
|
|
}
|
|
return patterns;
|
|
}
|
|
|
|
/** Workspace member directories declared by npm/yarn `workspaces` or pnpm-workspace.yaml. */
|
|
function listWorkspaceDirs(root, rootPkg = readJson(path.join(root, 'package.json'))) {
|
|
const declared = rootPkg?.workspaces;
|
|
const patterns = Array.isArray(declared)
|
|
? declared
|
|
: Array.isArray(declared?.packages)
|
|
? declared.packages
|
|
: [];
|
|
const dirs = new Set();
|
|
const excluded = new Set();
|
|
for (const pattern of [...patterns, ...readPnpmWorkspacePatterns(root)]) {
|
|
if (typeof pattern !== 'string') continue;
|
|
if (pattern.startsWith('!')) {
|
|
for (const dir of expand(root, pattern.slice(1))) excluded.add(dir);
|
|
} else {
|
|
for (const dir of expand(root, pattern)) dirs.add(dir);
|
|
}
|
|
}
|
|
return [...dirs].filter((dir) => !excluded.has(dir));
|
|
}
|
|
|
|
/**
|
|
* The `enriched-markdown` block that governs this install's downloads: the block of
|
|
* the directory the install ran from, else the flags every consuming workspace
|
|
* member agrees on. `{}` means download everything.
|
|
*/
|
|
function resolveConsumerConfig(initCwd) {
|
|
if (!initCwd) return {};
|
|
const rootPkg = readJson(path.join(initCwd, 'package.json'));
|
|
const rootConfig = configOf(rootPkg);
|
|
if (rootConfig) return rootConfig;
|
|
|
|
const consumers = listWorkspaceDirs(initCwd, rootPkg)
|
|
.map((dir) => readJson(path.join(dir, 'package.json')))
|
|
.filter(dependsOnPackage);
|
|
if (consumers.length === 0) return {};
|
|
|
|
// Only `=== false` merges, so non-boolean keys such as `codeHighlightLanguages`
|
|
// drop out on their own and new flags need no edit here.
|
|
const merged = {};
|
|
const keys = new Set(consumers.flatMap((pkg) => Object.keys(configOf(pkg) ?? {})));
|
|
for (const key of keys) {
|
|
if (consumers.every((pkg) => configOf(pkg)?.[key] === false)) merged[key] = false;
|
|
}
|
|
return merged;
|
|
}
|
|
|
|
// `--resolve-config` prints the lookup result and exits, for tests and debugging.
|
|
if (process.argv.includes('--resolve-config')) {
|
|
const initCwd = process.env.INIT_CWD;
|
|
console.log(JSON.stringify({
|
|
workspaceDirs: initCwd ? listWorkspaceDirs(initCwd) : [],
|
|
config: resolveConsumerConfig(initCwd),
|
|
}));
|
|
process.exit(0);
|
|
}
|
|
|
|
const grammarManifest = path.join(PKG_ROOT, 'cpp/highlight/grammar-versions.json');
|
|
if (!fs.existsSync(grammarManifest)) {
|
|
console.log(`${LOG} grammar-versions.json not found; skipping postinstall.`);
|
|
console.log(`${LOG} This is expected in monorepo development (use \`yarn prepare\`).`);
|
|
process.exit(0);
|
|
}
|
|
|
|
// Consumer opt-out. The consumer declares which heavy native assets to skip
|
|
// downloading via an `enriched-markdown` block in package.json:
|
|
//
|
|
// { "enriched-markdown": { "enableCodeHighlight": false, "enableMath": false } }
|
|
//
|
|
// Both features default to enabled (opt-out, not opt-in). The block is read from
|
|
// INIT_CWD (where the install ran) and, in a monorepo whose root has no block, from
|
|
// the workspace members that depend on this package -- see the lookup above.
|
|
// Any resolution failure falls back to downloading everything: a skipped download
|
|
// is far cheaper to recover from than a silently missing feature.
|
|
//
|
|
// The native build reads the *app* package.json directly (via the podspec's
|
|
// installation root / gradle's build root -- see the podspecs and android/build.gradle),
|
|
// which is a separate per-app decision. It reconciles that flag with asset presence on
|
|
// disk: an explicit opt-in with the asset missing fails loud, a default-on with it
|
|
// missing degrades to a clean build.
|
|
const consumerConfig = resolveConsumerConfig(process.env.INIT_CWD);
|
|
|
|
// This script only decides what to DOWNLOAD into node_modules. The native build
|
|
// (podspec + build.gradle) reads the app package.json directly to decide what to
|
|
// compile/link, so nothing is written here for it to consume. The `codeHighlightLanguages`
|
|
// subset is a build-time concern (a language subset is selected from the full downloaded
|
|
// grammar set), so it is intentionally not read here. ENV vars are a deprecated fallback,
|
|
// honored only when package.json has no explicit value.
|
|
let enableCodeHighlight = consumerConfig.enableCodeHighlight;
|
|
let enableMath = consumerConfig.enableMath;
|
|
|
|
if (enableCodeHighlight === undefined && process.env.ENRICHED_MARKDOWN_ENABLE_CODE_HIGHLIGHT) {
|
|
console.warn(`${LOG} DEPRECATED: ENRICHED_MARKDOWN_ENABLE_CODE_HIGHLIGHT env var will be removed in a future version. Configure via "enriched-markdown".enableCodeHighlight in your package.json instead.`);
|
|
enableCodeHighlight = process.env.ENRICHED_MARKDOWN_ENABLE_CODE_HIGHLIGHT !== '0';
|
|
}
|
|
if (enableMath === undefined && process.env.ENRICHED_MARKDOWN_ENABLE_MATH) {
|
|
console.warn(`${LOG} DEPRECATED: ENRICHED_MARKDOWN_ENABLE_MATH env var will be removed in a future version. Configure via "enriched-markdown".enableMath in your package.json instead.`);
|
|
enableMath = process.env.ENRICHED_MARKDOWN_ENABLE_MATH !== '0';
|
|
}
|
|
|
|
enableCodeHighlight = enableCodeHighlight !== false;
|
|
enableMath = enableMath !== false;
|
|
|
|
if (!enableCodeHighlight && !enableMath) {
|
|
console.log(`${LOG} both code highlighting and math are disabled via package.json ("enriched-markdown"); skipping postinstall.`);
|
|
process.exit(0);
|
|
}
|
|
|
|
console.log(`${LOG} restoring vendored native assets ...`);
|
|
|
|
const vendorGrammars = path.join(PKG_ROOT, 'vendor-grammars.mjs');
|
|
const vendorRatex = path.join(PKG_ROOT, 'vendor-ratex.mjs');
|
|
const vendorDir = path.join(PKG_ROOT, 'cpp/highlight/vendor');
|
|
const ratexManifest = path.join(PKG_ROOT, 'ratex-version.json');
|
|
const iosVendor = path.join(PKG_ROOT, 'ios/vendor');
|
|
|
|
let failed = false;
|
|
|
|
// Tree-sitter runtime + grammar C sources
|
|
if (enableCodeHighlight) {
|
|
const r1 = spawnSync(process.execPath, [
|
|
vendorGrammars,
|
|
'--from-npm',
|
|
'--vendor-dir', vendorDir,
|
|
'--manifest', grammarManifest,
|
|
], { stdio: 'inherit' });
|
|
if (r1.status !== 0) {
|
|
console.warn(`${LOG} WARNING: vendor-grammars failed. Code highlighting may not work.`);
|
|
failed = true;
|
|
}
|
|
} else {
|
|
console.log(`${LOG} code highlighting disabled via package.json ("enriched-markdown".enableCodeHighlight = false); skipping tree-sitter grammars.`);
|
|
}
|
|
|
|
// RaTeX XCFramework + Swift sources + fonts (iOS math)
|
|
if (!enableMath) {
|
|
console.log(`${LOG} math disabled via package.json ("enriched-markdown".enableMath = false); skipping RaTeX.`);
|
|
} else if (fs.existsSync(ratexManifest) && fs.existsSync(vendorRatex)) {
|
|
const r2 = spawnSync(process.execPath, [
|
|
vendorRatex,
|
|
'--manifest', ratexManifest,
|
|
'--output', iosVendor,
|
|
], { stdio: 'inherit' });
|
|
if (r2.status !== 0) {
|
|
console.warn(`${LOG} WARNING: vendor-ratex failed. iOS math rendering may not work.`);
|
|
failed = true;
|
|
}
|
|
} else {
|
|
// Math is on (explicitly or by default) but the vendor script or its manifest is
|
|
// absent, so nothing was downloaded. Never fall through silently -- the iOS build
|
|
// would otherwise just quietly ship without math. Warn like any other failure.
|
|
console.warn(`${LOG} WARNING: math is enabled but the RaTeX vendor script or manifest is missing ` +
|
|
`(${vendorRatex}, ${ratexManifest}); iOS math rendering will be unavailable.`);
|
|
failed = true;
|
|
}
|
|
|
|
if (failed) {
|
|
console.warn(
|
|
`${LOG} Some downloads failed. Ensure network access to registry.npmjs.org and github.com, ` +
|
|
`then re-run: node node_modules/react-native-enriched-markdown/postinstall.mjs`
|
|
);
|
|
}
|
|
|
|
console.log(`${LOG} postinstall complete.`);
|