-
Notifications
You must be signed in to change notification settings - Fork 0
fix: address PR #492 review comments (Y.Xml extract, testConnection, useRef) #493
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import * as Y from "yjs"; | ||
| import { buildContentPreview, extractTextFromYXml } from "./extractPlainTextFromYXml.js"; | ||
|
|
||
| describe("extractTextFromYXml", () => { | ||
| it("does not insert a newline inside a paragraph between inline bold and following text", () => { | ||
| const doc = new Y.Doc(); | ||
| doc.transact(() => { | ||
| const fragment = doc.getXmlFragment("default"); | ||
| const paragraph = new Y.XmlElement("paragraph"); | ||
| fragment.push([paragraph]); | ||
| const t1 = new Y.XmlText(); | ||
| t1.insert(0, "Hello "); | ||
| paragraph.push([t1]); | ||
| const bold = new Y.XmlElement("bold"); | ||
| paragraph.push([bold]); | ||
| const tBold = new Y.XmlText(); | ||
| tBold.insert(0, "world"); | ||
| bold.push([tBold]); | ||
| const t2 = new Y.XmlText(); | ||
| t2.insert(0, "!"); | ||
| paragraph.push([t2]); | ||
| }); | ||
|
|
||
| const plain = extractTextFromYXml(doc.getXmlFragment("default")).trim(); | ||
| expect(plain).not.toMatch(/world\s*\n\s*!/); | ||
| expect(plain.replace(/\s+/g, " ").trim()).toBe("Hello world !"); | ||
| }); | ||
|
|
||
| it("separates block-level paragraphs with newlines", () => { | ||
| const doc = new Y.Doc(); | ||
| doc.transact(() => { | ||
| const fragment = doc.getXmlFragment("default"); | ||
| const p1 = new Y.XmlElement("paragraph"); | ||
| fragment.push([p1]); | ||
| const a = new Y.XmlText(); | ||
| a.insert(0, "First"); | ||
| p1.push([a]); | ||
| const p2 = new Y.XmlElement("paragraph"); | ||
| fragment.push([p2]); | ||
| const b = new Y.XmlText(); | ||
| b.insert(0, "Second"); | ||
| p2.push([b]); | ||
| }); | ||
|
|
||
| const plain = extractTextFromYXml(doc.getXmlFragment("default")).trim(); | ||
| expect(plain).toMatch(/First/); | ||
| expect(plain).toMatch(/Second/); | ||
| expect(plain.includes("First") && plain.includes("Second")).toBe(true); | ||
| expect(/\n/.test(plain)).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("buildContentPreview", () => { | ||
| it("collapses whitespace and truncates", () => { | ||
| expect(buildContentPreview(" a \n b ")).toBe("a b"); | ||
| const long = "x".repeat(200); | ||
| const prev = buildContentPreview(long); | ||
| expect(prev.endsWith("...")).toBe(true); | ||
| expect(prev.length).toBeLessThanOrEqual(124); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,65 @@ | ||||||
| import * as Y from "yjs"; | ||||||
|
|
||||||
| /** | ||||||
| * TipTap / ProseMirror のマーク相当で、Y.Xml 上に子要素として現れるインライン名。 | ||||||
| * 兄弟インライン間では改行ではなくスペースを挟み、プレビュー用テキストの不自然な改行を防ぐ。 | ||||||
| * | ||||||
| * Mark-like XmlElement names in the Y.Xml tree; use a space (not a newline) between | ||||||
| * inline siblings so plain-text extraction does not split words (e.g. `Hello world!`). | ||||||
| */ | ||||||
| const INLINE_XML_ELEMENT_NAMES = new Set<string>([ | ||||||
| "bold", | ||||||
| "italic", | ||||||
| "strike", | ||||||
| "code", | ||||||
| "link", | ||||||
| "underline", | ||||||
| "highlight", | ||||||
| "subscript", | ||||||
| "superscript", | ||||||
| "textStyle", | ||||||
| ]); | ||||||
|
|
||||||
| /** | ||||||
| * インライン要素かどうかを nodeName で判定する。 | ||||||
| * Determine whether an XmlElement is inline-only (no trailing newline after its subtree). | ||||||
| */ | ||||||
| function isInlineXmlElement(node: Y.XmlElement): boolean { | ||||||
| return INLINE_XML_ELEMENT_NAMES.has(node.nodeName); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Y.Doc の XmlFragment(または XmlElement 根)からプレーンテキストを再帰的に抽出する。 | ||||||
| * Recursively extract plain text from a Y.XmlFragment or Y.XmlElement subtree. | ||||||
| */ | ||||||
| export function extractTextFromYXml(node: Y.XmlFragment | Y.XmlElement): string { | ||||||
| let text = ""; | ||||||
|
|
||||||
| for (let i = 0; i < node.length; i++) { | ||||||
| const child = node.get(i); | ||||||
| if (child instanceof Y.XmlText) { | ||||||
| text += child.toString(); | ||||||
| } else if (child instanceof Y.XmlElement) { | ||||||
| const inner = extractTextFromYXml(child); | ||||||
| const suffix = isInlineXmlElement(child) ? " " : "\n"; | ||||||
| text += inner + suffix; | ||||||
| } | ||||||
| } | ||||||
| return text; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * プレビュー文字列の最大長(DB の content_preview と一致させる)。 | ||||||
| * Max length for content preview (aligned with `pages.content_preview`). | ||||||
| */ | ||||||
| export const CONTENT_PREVIEW_MAX_LENGTH = 120; | ||||||
|
|
||||||
| /** | ||||||
| * プレーンテキストからコンテンツプレビュー(先頭120文字)を生成する。 | ||||||
| * Generate content preview (first 120 chars) from plain text. | ||||||
| */ | ||||||
| export function buildContentPreview(text: string): string { | ||||||
| const trimmed = text.trim().replace(/\s+/g, " "); | ||||||
| if (trimmed.length <= CONTENT_PREVIEW_MAX_LENGTH) return trimmed; | ||||||
| return trimmed.slice(0, CONTENT_PREVIEW_MAX_LENGTH).trim() + "..."; | ||||||
|
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. DBの
Suggested change
|
||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Yjsの共有型(XmlFragmentやXmlElement)において、$O(N^2)$ の計算量になります。これは、
node.lengthとnode.get(i)を組み合わせたループはlengthとgetの両方が内部の連結リストを走査するためです。ドキュメントが大きくなった際のパフォーマンス低下を避けるため、node.toArray()を使用して配列に変換してからループを回すか、firstChildとnextSiblingを使用したイテレーションを推奨します。