-
Notifications
You must be signed in to change notification settings - Fork 492
Notes Markdown 编辑器的 4 项修复 #3214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9572907
6c842a0
519b2b1
d1bd7f6
18d1bd3
8c78fdf
9baa5f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ import { | |
| thematicBreakPlugin, | ||
| } from "@mdxeditor/editor"; | ||
| import { HighlightStyle, syntaxHighlighting } from "@codemirror/language"; | ||
| import { Prec } from "@codemirror/state"; | ||
| import { tooltips } from "@codemirror/view"; | ||
| import { tags } from "@lezer/highlight"; | ||
| import { AlertTriangle, ExternalLink } from "lucide-react"; | ||
| import { | ||
|
|
@@ -302,7 +304,6 @@ const NOTE_CODE_BLOCK_LANGUAGES = { | |
| sh: "Shell", | ||
| shell: "Shell", | ||
| sql: "SQL", | ||
| tex: "TeX", | ||
| toml: "TOML", | ||
| ts: "TypeScript", | ||
| tsx: "TypeScript (React)", | ||
|
|
@@ -330,7 +331,14 @@ const noteCodeHighlightStyle = HighlightStyle.define([ | |
| { tag: tags.invalid, class: "netcatty-code-token-invalid" }, | ||
| ]); | ||
|
|
||
| const NOTE_CODE_MIRROR_EXTENSIONS = [syntaxHighlighting(noteCodeHighlightStyle)]; | ||
| const NOTE_CODE_MIRROR_EXTENSIONS = [ | ||
| syntaxHighlighting(noteCodeHighlightStyle), | ||
| Prec.highest( | ||
| tooltips({ | ||
| parent: typeof document !== "undefined" ? document.body : undefined, | ||
| }), | ||
| ), | ||
| ]; | ||
|
|
||
| type RectLike = Pick<DOMRect, "bottom" | "height" | "left" | "top" | "width">; | ||
|
|
||
|
|
@@ -807,14 +815,35 @@ export const annotateNoteCodeBlockDeleteButtons = (container: HTMLElement): void | |
| export const isNoteMathLanguageLabel = (value: string): boolean => { | ||
| const normalized = value.toLowerCase().trim(); | ||
| if (!normalized) return false; | ||
| return normalized === "latex" | ||
| return ( | ||
| normalized === "latex" | ||
| || normalized === "tex" | ||
| || /(?:^|\s)language-(?:latex|tex)(?:\s|$)/.test(normalized); | ||
| || normalized === "math" | ||
| || normalized === "katex" | ||
| || normalized === "formula" | ||
| || normalized === "公式" | ||
| || normalized === "math (latex)" | ||
| || /(?:^|\s)language-(?:latex|tex|math|katex|formula)(?:\s|$)/.test(normalized) | ||
| || /^(?:math|latex|tex|katex|formula|公式)(?:\s|\(|$)/.test(normalized) | ||
| ); | ||
| }; | ||
|
|
||
| export const shouldRenderNoteMathFormula = ( | ||
| languageLabel: string, | ||
| ): boolean => isNoteMathLanguageLabel(languageLabel); | ||
| content?: string, | ||
| ): boolean => { | ||
| if (isNoteMathLanguageLabel(languageLabel)) return true; | ||
| if (content) { | ||
| const trimmed = content.trim(); | ||
| if (trimmed.startsWith("$$") && trimmed.endsWith("$$") && trimmed.length >= 4) { | ||
| return true; | ||
| } | ||
| if (trimmed.startsWith("\\[") && trimmed.endsWith("\\]") && trimmed.length >= 4) { | ||
| return true; | ||
| } | ||
|
Comment on lines
+836
to
+843
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a plain-text or unlabeled fenced block literally contains Useful? React with 👍 / 👎. |
||
| } | ||
| return false; | ||
| }; | ||
|
|
||
| export const annotateMathFormulaBlocks = (container: HTMLElement, editorMode: string): void => { | ||
| container.querySelectorAll('[class*="_codeMirrorWrapper_"], pre').forEach((wrapper) => { | ||
|
|
@@ -833,7 +862,7 @@ export const annotateMathFormulaBlocks = (container: HTMLElement, editorMode: st | |
|
|
||
| const text = getCodeMirrorBlockText(wrapper).trim(); | ||
|
|
||
| const isMathBlock = shouldRenderNoteMathFormula(lang); | ||
| const isMathBlock = shouldRenderNoteMathFormula(lang, text); | ||
| if (!isMathBlock) { | ||
| const existingPreview = wrapper.querySelector(".netcatty-math-formula-preview"); | ||
| if (existingPreview) existingPreview.remove(); | ||
|
|
@@ -858,7 +887,11 @@ export const annotateMathFormulaBlocks = (container: HTMLElement, editorMode: st | |
|
|
||
| if (preview.dataset.formulaSource !== formulaSource) { | ||
| preview.dataset.formulaSource = formulaSource; | ||
| preview.innerHTML = renderNoteMathFormula(formulaSource); | ||
| try { | ||
| preview.innerHTML = renderNoteMathFormula(formulaSource); | ||
| } catch { | ||
| preview.textContent = formulaSource; | ||
| } | ||
| } | ||
|
|
||
| if (editorMode === "preview") { | ||
|
|
@@ -868,6 +901,21 @@ export const annotateMathFormulaBlocks = (container: HTMLElement, editorMode: st | |
| } | ||
| }); | ||
|
|
||
| if (editorMode === "preview") { | ||
| container.querySelectorAll("p").forEach((p) => { | ||
| const pText = p.textContent?.trim() || ""; | ||
| if (pText.startsWith("$$") && pText.endsWith("$$") && pText.length >= 4) { | ||
| if (p.querySelector(".katex, math")) return; | ||
| const formula = pText.slice(2, -2).trim(); | ||
| try { | ||
| p.innerHTML = renderNoteMathFormula(formula); | ||
| p.classList.add("netcatty-math-block-p"); | ||
| } catch { | ||
| // ignore | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| const deleteLexicalTextRange = (range: Range, onUpdate: () => void): boolean => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Existing or imported notes can contain fenced
texblocks, and the surrounding detection and CSS still explicitly supporttex, but removing it fromNOTE_CODE_BLOCK_LANGUAGESmeanscodeMirrorPluginno longer recognizes or offers that language through its supported-language registry. Such blocks can therefore lose their CodeMirror editor/formula-preview path; retain thetex: "TeX"alias alongsidelatexfor backward compatibility.Useful? React with 👍 / 👎.