Skip to content

Commit 4c50a72

Browse files
fix(mobile): preserve Windows paths during native parsing
1 parent c77071e commit 4c50a72

8 files changed

Lines changed: 181 additions & 73 deletions

File tree

‎apps/mobile/deps/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
The Enriched Markdown tarball comes from the [`fork/t3-mobile` branch](https://github.com/pingdotgg/enriched-markdown/tree/fork/t3-mobile). Its `t3-fork.json` records the exact source commit and upstream base. File/skill link recognition, back navigation from horizontal blocks, table-image presses, and code-block headers use upstream implementations. The remaining fork adds clipboard metadata, parsed asset manifests, signed image source resolution, React media slots, and development-host autolinks.
22

33
To replace it, commit the fork changes, run `yarn install --immutable` and `yarn workspace react-native-enriched-markdown prepare` in the fork checkout, then `node scripts/pack-t3.mjs /path/to/t3code/apps/mobile/deps`. Bump the fork package version for each replacement, update the tarball path in `apps/mobile/package.json` and `pnpm-workspace.yaml`'s `allowBuilds`, and run `vp i` to update the lockfile. Native clients need rebuilding when this dependency changes.
4+
5+
The pnpm patch to Enriched Markdown preserves Windows drive-path separators in prose during native parsing. Keep it when replacing the tarball, until the fork includes it. This native parser change requires rebuilding installed clients; a JavaScript-only update does not include it.

‎apps/mobile/src/lib/markdownDocument.ts‎

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -206,32 +206,3 @@ export function markdownWithTableBreaks(markdown: string): string {
206206
export function parseMarkdownDocument(markdown: string): Root {
207207
return parser.parse(markdownWithHtml(markdown));
208208
}
209-
210-
export function markdownWithWindowsPaths(markdown: string): string {
211-
if (!markdown.includes("\\") || !/[A-Za-z]:/.test(markdown)) return markdown;
212-
const offsets: number[] = [];
213-
const document = parser()
214-
.use(remarkKeepWindowsPaths, { onPreservedEscape: (offset) => offsets.push(offset) })
215-
.parse(markdown);
216-
const parts: string[] = [];
217-
let previous = 0;
218-
let index = 0;
219-
// Escape callbacks also see buffered link destinations; only final text nodes are prose.
220-
const visit = (node: MarkdownNode) => {
221-
if (node.type === "text") {
222-
const start = node.position?.start.offset;
223-
const end = node.position?.end.offset;
224-
if (start === undefined || end === undefined) return;
225-
while (index < offsets.length && offsets[index]! < start) index += 1;
226-
while (index < offsets.length && offsets[index]! < end) {
227-
const offset = offsets[index++]!;
228-
// The native parser needs both a literal separator and the original punctuation escape.
229-
parts.push(markdown.slice(previous, offset), "\\\\");
230-
previous = offset;
231-
}
232-
} else if ("children" in node) node.children.forEach(visit);
233-
};
234-
visit(document);
235-
parts.push(markdown.slice(previous));
236-
return parts.join("");
237-
}

‎apps/mobile/src/lib/markdownHtml.test.ts‎

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import { describe, expect, it } from "vite-plus/test";
2-
import {
3-
markdownHtmlContent,
4-
markdownWithHtml,
5-
markdownWithWindowsPaths,
6-
parseMarkdownDocument,
7-
} from "./markdownDocument";
8-
import { unified } from "unified";
9-
import remarkParse from "remark-parse";
10-
import remarkGfm from "remark-gfm";
2+
import { markdownHtmlContent, markdownWithHtml, parseMarkdownDocument } from "./markdownDocument";
113
import { markdownImageSources } from "./markdownImageSources";
124

135
describe("HTML in native Markdown", () => {
@@ -81,18 +73,12 @@ describe("HTML in native Markdown", () => {
8173
});
8274

8375
describe("Windows drive paths in mobile Markdown documents", () => {
84-
const sourceParser = unified().use(remarkParse).use(remarkGfm);
85-
const parsers = [
86-
parseMarkdownDocument,
87-
(markdown: string) => sourceParser.parse(markdownWithWindowsPaths(markdown)),
88-
];
8976
it("keeps backslashes before punctuation in prose paths", () => {
9077
const markdown = String.raw`Saved to C:\Users\me\.t3\_build\shot.png and C:/work\.config and C:\folder\(draft.`;
9178

92-
for (const parse of parsers)
93-
expect(parse(markdown).children).toMatchObject([
94-
{ type: "paragraph", children: [{ type: "text", value: markdown }] },
95-
]);
79+
expect(parseMarkdownDocument(markdown).children).toMatchObject([
80+
{ type: "paragraph", children: [{ type: "text", value: markdown }] },
81+
]);
9682
});
9783

9884
it("keeps ordinary Markdown escapes and inline and fenced code unchanged around drive paths", () => {
@@ -105,21 +91,39 @@ describe("Windows drive paths in mobile Markdown documents", () => {
10591
"```",
10692
].join("\n");
10793

108-
for (const parse of parsers)
109-
expect(parse(markdown).children).toMatchObject([
110-
{
111-
type: "paragraph",
112-
children: [
113-
{ type: "text", value: String.raw`See C:\x and *this* and Note:*that*. Code: ` },
114-
{ type: "inlineCode", value: code },
115-
],
116-
},
117-
{ type: "code", lang: "text", value: code },
118-
]);
94+
expect(parseMarkdownDocument(markdown).children).toMatchObject([
95+
{
96+
type: "paragraph",
97+
children: [
98+
{ type: "text", value: String.raw`See C:\x and *this* and Note:*that*. Code: ` },
99+
{ type: "inlineCode", value: code },
100+
],
101+
},
102+
{ type: "code", lang: "text", value: code },
103+
]);
119104

120105
const links = String.raw`[file](C:\dir\.config "C:\dir\_title") and ![image](C:\dir\.png)
121106
122107
[file]: C:\dir\.config`;
123-
expect(sourceParser.parse(markdownWithWindowsPaths(links))).toEqual(sourceParser.parse(links));
108+
expect(parseMarkdownDocument(links).children).toMatchObject([
109+
{
110+
type: "paragraph",
111+
children: [
112+
{ type: "link", url: String.raw`C:\dir\.config`, title: String.raw`C:\dir\_title` },
113+
{ type: "text", value: " and " },
114+
{ type: "image", url: String.raw`C:\dir\.png` },
115+
],
116+
},
117+
{ type: "definition", identifier: "file", url: String.raw`C:\dir\.config` },
118+
]);
119+
for (const suffix of ["", "[]"]) {
120+
const references =
121+
String.raw`[C:\a\.b]` + suffix + "\n\n" + String.raw`[C:\a\.b]: https://x.test`;
122+
const [paragraph, definition] = parseMarkdownDocument(references).children;
123+
expect(paragraph).toMatchObject({ type: "paragraph", children: [{ type: "linkReference" }] });
124+
expect(definition).toMatchObject({ type: "definition", url: "https://x.test" });
125+
if (paragraph?.type === "paragraph" && definition?.type === "definition")
126+
expect(paragraph.children[0]).toMatchObject({ identifier: definition.identifier });
127+
}
124128
});
125129
});

‎apps/mobile/src/native/EnrichedMarkdownText.tsx‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import {
44
formatComposerContextHref,
55
formatComposerContextReference,
66
} from "@t3tools/shared/composerContextReferences";
7-
import {
8-
markdownWithHtml,
9-
markdownWithTableBreaks,
10-
markdownWithWindowsPaths,
11-
} from "../lib/markdownDocument";
7+
import { markdownWithHtml, markdownWithTableBreaks } from "../lib/markdownDocument";
128
import { useCallback, useMemo, useState, type ReactNode } from "react";
139
import { Platform, Share, StyleSheet, View } from "react-native";
1410
import {
@@ -145,7 +141,7 @@ function enrichedStyle(
145141
export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
146142
const { renderImage, resolveImageSource } = props;
147143
const markdown = useMemo(
148-
() => markdownWithWindowsPaths(markdownWithTableBreaks(markdownWithHtml(props.markdown))),
144+
() => markdownWithTableBreaks(markdownWithHtml(props.markdown)),
149145
[props.markdown],
150146
);
151147
const { themeAppearance } = useAppearancePreferences();

‎packages/shared/src/markdownWindowsPaths.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,13 @@ interface EscapeCompileContext {
3333
* Prose paths end at whitespace: past that, nothing tells a path from a sentence, and
3434
* `C:\x and \*this\*` must still read `*this*`.
3535
*/
36-
export function remarkKeepWindowsPaths(
37-
this: Processor,
38-
options?: { onPreservedEscape?: (offset: number) => void },
39-
) {
40-
function keepWindowsPathEscape(this: EscapeCompileContext, token: { start: { offset: number } }) {
36+
export function remarkKeepWindowsPaths(this: Processor) {
37+
function keepWindowsPathEscape(this: EscapeCompileContext) {
4138
const text = this.stack.at(-1)?.children?.at(-1);
4239
if (text?.type !== "text" || text.value === undefined) return;
4340
const before = text.value.slice(0, -1);
4441
if (UNFINISHED_DRIVE_PATH.test(before)) {
4542
text.value = `${before}\\${text.value.slice(-1)}`;
46-
options?.onPreservedEscape?.(token.start.offset);
4743
}
4844
}
4945

‎patches/[email protected]‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
diff --git a/cpp/enrmrkd/enrmrkd.h b/cpp/enrmrkd/enrmrkd.h
2+
--- a/cpp/enrmrkd/enrmrkd.h
3+
+++ b/cpp/enrmrkd/enrmrkd.h
4+
@@ -244,7 +244,10 @@
5+
6+
/* Text is inside an equation. This is processed the same way as inlined code
7+
* spans (`code`). */
8+
- ENRMRKD_TEXT_LATEXMATH
9+
+ ENRMRKD_TEXT_LATEXMATH,
10+
+
11+
+ /* An escaped punctuation character, after syntax and reference resolution. */
12+
+ ENRMRKD_TEXT_ESCAPED
13+
} ENRMRKD_TEXTTYPE;
14+
15+
16+
diff --git a/cpp/enrmrkd/enrmrkd.c b/cpp/enrmrkd/enrmrkd.c
17+
--- a/cpp/enrmrkd/enrmrkd.c
18+
+++ b/cpp/enrmrkd/enrmrkd.c
19+
@@ -4785,7 +4785,8 @@
20+
if(ISNEWLINE(mark->beg+1))
21+
enforce_hardbreak = 1;
22+
else
23+
- ENRMRKD_TEXT(text_type, STR(mark->beg+1), 1);
24+
+ ENRMRKD_TEXT(text_type == ENRMRKD_TEXT_NORMAL ? ENRMRKD_TEXT_ESCAPED : text_type,
25+
+ STR(mark->beg+1), 1);
26+
break;
27+
28+
case ' ': /* Non-trivial space. */
29+
diff --git a/cpp/parser/MD4CParser.cpp b/cpp/parser/MD4CParser.cpp
30+
--- a/cpp/parser/MD4CParser.cpp
31+
+++ b/cpp/parser/MD4CParser.cpp
32+
@@ -1,11 +1,13 @@
33+
#include "MD4CParser.hpp"
34+
#include "enrmrkd.h"
35+
+#include <algorithm>
36+
#include <cctype>
37+
#include <cstdlib>
38+
extern "C" {
39+
#include "enrmrkd-entity.h"
40+
}
41+
#include <cstring>
42+
+#include <string_view>
43+
#include <vector>
44+
45+
namespace Markdown {
46+
@@ -71,6 +73,30 @@
47+
appendCodepoint(output, codepoint);
48+
}
49+
50+
+bool isPathWhitespace(const std::string &text, size_t at) {
51+
+ const auto c = static_cast<unsigned char>(text[at]);
52+
+ if (c < 128)
53+
+ return c == ' ' || (c >= '\t' && c <= '\r');
54+
+ if (c != 0xC2 && c != 0xE1 && c != 0xE2 && c != 0xE3 && c != 0xEF)
55+
+ return false;
56+
+ // Match JavaScript's \s, including non-ASCII spaces in decoded prose.
57+
+ constexpr std::string_view spaces[] = {
58+
+ "\u00a0", "\u1680", "\u2000", "\u2001", "\u2002", "\u2003", "\u2004", "\u2005", "\u2006", "\u2007",
59+
+ "\u2008", "\u2009", "\u200a", "\u2028", "\u2029", "\u202f", "\u205f", "\u3000", "\ufeff"};
60+
+ for (const auto space : spaces)
61+
+ if (text.compare(at, space.size(), space) == 0)
62+
+ return true;
63+
+ return false;
64+
+}
65+
+
66+
+bool isDriveLetter(unsigned char c) {
67+
+ return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
68+
+}
69+
+
70+
+bool isWordCharacter(unsigned char c) {
71+
+ return isDriveLetter(c) || (c >= '0' && c <= '9') || c == '_';
72+
+}
73+
+
74+
struct HtmlTag {
75+
std::string name;
76+
std::unordered_map<std::string, std::string> attributes;
77+
@@ -84,6 +110,8 @@
78+
std::shared_ptr<MarkdownASTNode> root;
79+
std::vector<std::shared_ptr<MarkdownASTNode>> nodeStack;
80+
std::string currentText;
81+
+ size_t pathScanned = 0;
82+
+ bool inWindowsPath = false;
83+
const char *inputText = nullptr;
84+
bool inHtmlBlock = false;
85+
std::string htmlBlockContent;
86+
@@ -108,6 +136,8 @@
87+
nodeStack.reserve(std::min(estimatedDepth, static_cast<size_t>(128)));
88+
nodeStack.push_back(root);
89+
currentText.clear();
90+
+ pathScanned = 0;
91+
+ inWindowsPath = false;
92+
currentText.reserve(256);
93+
inHtmlBlock = false;
94+
htmlBlockContent.clear();
95+
@@ -120,6 +150,26 @@
96+
nodeStack.back()->addChild(std::move(textNode));
97+
currentText.clear();
98+
}
99+
+ pathScanned = 0;
100+
+ inWindowsPath = false;
101+
+ }
102+
+
103+
+ bool endsInWindowsPath() {
104+
+ // Inspect only appended text, so many escapes cannot rescan a growing paragraph.
105+
+ for (; pathScanned < currentText.size(); ++pathScanned) {
106+
+ const size_t at = pathScanned;
107+
+ if (isPathWhitespace(currentText, at)) {
108+
+ inWindowsPath = false;
109+
+ } else if (!inWindowsPath && (currentText[at] == '/' || currentText[at] == '\\') &&
110+
+ at >= 2 && currentText[at - 1] == ':' && isDriveLetter(currentText[at - 2]) &&
111+
+ (at == 2 || !isWordCharacter(currentText[at - 3]))) {
112+
+ inWindowsPath = true;
113+
+ }
114+
+ }
115+
+ const size_t end = currentText.size();
116+
+ return inWindowsPath ||
117+
+ (end >= 2 && currentText[end - 1] == ':' && isDriveLetter(currentText[end - 2]) &&
118+
+ (end == 2 || !isWordCharacter(currentText[end - 3])));
119+
}
120+
121+
void pushNode(std::shared_ptr<MarkdownASTNode> node) {
122+
@@ -504,6 +554,15 @@
123+
124+
if (type == ENRMRKD_TEXT_ENTITY) {
125+
appendCharacterReference(impl->currentText, text, size);
126+
+ return 0;
127+
+ }
128+
+
129+
+ if (type == ENRMRKD_TEXT_ESCAPED) {
130+
+ const bool inImage = std::any_of(impl->nodeStack.begin(), impl->nodeStack.end(),
131+
+ [](const auto &node) { return node->type == NodeType::Image; });
132+
+ if (!inImage && impl->endsInWindowsPath())
133+
+ impl->currentText += '\\';
134+
+ impl->currentText.append(text, size);
135+
return 0;
136+
}
137+

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pnpm-workspace.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ patchedDependencies:
331331
"@react-navigation/[email protected]": patches/@react-navigation%[email protected]
332332
333333
334+
334335
335336
336337
# Android: a nested WebView hands a vertical drag to its scrolling parent at the page's edge.

0 commit comments

Comments
 (0)