Revert "Fix markdown editor escaped list markers"
This reverts commit fbcd80948e.
This commit is contained in:
@@ -4,7 +4,6 @@ import remarkGfm from "remark-gfm";
|
|||||||
import { parseProjectMentionHref } from "@paperclipai/shared";
|
import { parseProjectMentionHref } from "@paperclipai/shared";
|
||||||
import { cn } from "../lib/utils";
|
import { cn } from "../lib/utils";
|
||||||
import { useTheme } from "../context/ThemeContext";
|
import { useTheme } from "../context/ThemeContext";
|
||||||
import { normalizeMarkdownArtifacts } from "../lib/markdown";
|
|
||||||
|
|
||||||
interface MarkdownBodyProps {
|
interface MarkdownBodyProps {
|
||||||
children: string;
|
children: string;
|
||||||
@@ -115,7 +114,6 @@ function MermaidDiagramBlock({ source, darkMode }: { source: string; darkMode: b
|
|||||||
|
|
||||||
export function MarkdownBody({ children, className }: MarkdownBodyProps) {
|
export function MarkdownBody({ children, className }: MarkdownBodyProps) {
|
||||||
const { theme } = useTheme();
|
const { theme } = useTheme();
|
||||||
const normalizedMarkdown = normalizeMarkdownArtifacts(children);
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
@@ -156,7 +154,7 @@ export function MarkdownBody({ children, className }: MarkdownBodyProps) {
|
|||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{normalizedMarkdown}
|
{children}
|
||||||
</Markdown>
|
</Markdown>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ import {
|
|||||||
} from "@mdxeditor/editor";
|
} from "@mdxeditor/editor";
|
||||||
import { buildProjectMentionHref, parseProjectMentionHref } from "@paperclipai/shared";
|
import { buildProjectMentionHref, parseProjectMentionHref } from "@paperclipai/shared";
|
||||||
import { cn } from "../lib/utils";
|
import { cn } from "../lib/utils";
|
||||||
import { normalizeMarkdownArtifacts } from "../lib/markdown";
|
|
||||||
|
|
||||||
/* ---- Mention types ---- */
|
/* ---- Mention types ---- */
|
||||||
|
|
||||||
@@ -204,7 +203,7 @@ export const MarkdownEditor = forwardRef<MarkdownEditorRef, MarkdownEditorProps>
|
|||||||
}: MarkdownEditorProps, forwardedRef) {
|
}: MarkdownEditorProps, forwardedRef) {
|
||||||
const containerRef = useRef<HTMLDivElement>(null);
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
const ref = useRef<MDXEditorMethods>(null);
|
const ref = useRef<MDXEditorMethods>(null);
|
||||||
const latestValueRef = useRef(normalizeMarkdownArtifacts(value));
|
const latestValueRef = useRef(value);
|
||||||
const [uploadError, setUploadError] = useState<string | null>(null);
|
const [uploadError, setUploadError] = useState<string | null>(null);
|
||||||
const [isDragOver, setIsDragOver] = useState(false);
|
const [isDragOver, setIsDragOver] = useState(false);
|
||||||
const dragDepthRef = useRef(0);
|
const dragDepthRef = useRef(0);
|
||||||
@@ -282,10 +281,9 @@ export const MarkdownEditor = forwardRef<MarkdownEditorRef, MarkdownEditorProps>
|
|||||||
}, [hasImageUpload]);
|
}, [hasImageUpload]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const normalizedValue = normalizeMarkdownArtifacts(value);
|
if (value !== latestValueRef.current) {
|
||||||
if (normalizedValue !== latestValueRef.current) {
|
ref.current?.setMarkdown(value);
|
||||||
ref.current?.setMarkdown(normalizedValue);
|
latestValueRef.current = value;
|
||||||
latestValueRef.current = normalizedValue;
|
|
||||||
}
|
}
|
||||||
}, [value]);
|
}, [value]);
|
||||||
|
|
||||||
@@ -556,15 +554,11 @@ export const MarkdownEditor = forwardRef<MarkdownEditorRef, MarkdownEditorProps>
|
|||||||
>
|
>
|
||||||
<MDXEditor
|
<MDXEditor
|
||||||
ref={ref}
|
ref={ref}
|
||||||
markdown={normalizeMarkdownArtifacts(value)}
|
markdown={value}
|
||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
onChange={(next) => {
|
onChange={(next) => {
|
||||||
const normalizedNext = normalizeMarkdownArtifacts(next);
|
latestValueRef.current = next;
|
||||||
latestValueRef.current = normalizedNext;
|
onChange(next);
|
||||||
if (normalizedNext !== next) {
|
|
||||||
ref.current?.setMarkdown(normalizedNext);
|
|
||||||
}
|
|
||||||
onChange(normalizedNext);
|
|
||||||
}}
|
}}
|
||||||
onBlur={() => onBlur?.()}
|
onBlur={() => onBlur?.()}
|
||||||
className={cn("paperclip-mdxeditor", !bordered && "paperclip-mdxeditor--borderless")}
|
className={cn("paperclip-mdxeditor", !bordered && "paperclip-mdxeditor--borderless")}
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
|
||||||
import { normalizeMarkdownArtifacts } from "./markdown";
|
|
||||||
|
|
||||||
describe("normalizeMarkdownArtifacts", () => {
|
|
||||||
it("normalizes escaped unordered list markers and space entities", () => {
|
|
||||||
const input = "Here is a list:\n\n\\* foo \n\\- bar ";
|
|
||||||
const output = normalizeMarkdownArtifacts(input);
|
|
||||||
expect(output).toBe("Here is a list:\n\n* foo \n- bar ");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("does not rewrite escaped markers inside fenced code blocks", () => {
|
|
||||||
const input = "```md\n\\* keep literal \n\\- keep literal \n```";
|
|
||||||
expect(normalizeMarkdownArtifacts(input)).toBe(input);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("keeps escaped non-list syntax intact", () => {
|
|
||||||
const input = "\\*not-a-list";
|
|
||||||
expect(normalizeMarkdownArtifacts(input)).toBe(input);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
const FENCE_RE = /^\s*(`{3,}|~{3,})/;
|
|
||||||
const SPACE_ENTITY_RE = / /gi;
|
|
||||||
const ESCAPED_UNORDERED_LIST_RE = /^(\s{0,3})\\([*+-])([ \t]+)/;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Normalize markdown artifacts emitted by rich-text serialization so
|
|
||||||
* plain markdown list syntax remains usable in Paperclip editors.
|
|
||||||
*/
|
|
||||||
export function normalizeMarkdownArtifacts(markdown: string): string {
|
|
||||||
if (!markdown) return markdown;
|
|
||||||
|
|
||||||
const lines = markdown.split(/\r?\n/);
|
|
||||||
let inFence = false;
|
|
||||||
let fenceMarker: "`" | "~" | null = null;
|
|
||||||
let fenceLength = 0;
|
|
||||||
let changed = false;
|
|
||||||
|
|
||||||
const normalized = lines.map((line) => {
|
|
||||||
const fenceMatch = FENCE_RE.exec(line);
|
|
||||||
if (fenceMatch) {
|
|
||||||
const marker = fenceMatch[1];
|
|
||||||
if (!inFence) {
|
|
||||||
inFence = true;
|
|
||||||
fenceMarker = marker[0] as "`" | "~";
|
|
||||||
fenceLength = marker.length;
|
|
||||||
} else if (marker[0] === fenceMarker && marker.length >= fenceLength) {
|
|
||||||
inFence = false;
|
|
||||||
fenceMarker = null;
|
|
||||||
fenceLength = 0;
|
|
||||||
}
|
|
||||||
return line;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inFence) return line;
|
|
||||||
|
|
||||||
let next = line;
|
|
||||||
if (next.includes(" ")) {
|
|
||||||
next = next.replace(SPACE_ENTITY_RE, " ");
|
|
||||||
}
|
|
||||||
const unescaped = next.replace(ESCAPED_UNORDERED_LIST_RE, "$1$2$3");
|
|
||||||
if (unescaped !== line) changed = true;
|
|
||||||
return unescaped;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!changed) return markdown;
|
|
||||||
return normalized.join("\n");
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user