Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ function blockForNode(
isUnknown: false,
block: {
type: blockMapping.blockName,
parents: findParents(adapter, nodePath, node),
parents: findParents(adapter, nodePath, node, blockMapping.isEmbed || false),
attrs,
isEmbed: blockMapping.isEmbed || false,
},
Expand All @@ -383,7 +383,7 @@ function blockForNode(
isUnknown: false,
block: {
type: blockMapping.blockName,
parents: findParents(adapter, nodePath, node),
parents: findParents(adapter, nodePath, node, true),
attrs,
isEmbed: true,
},
Expand Down Expand Up @@ -558,14 +558,15 @@ function findParents(
adapter: SchemaAdapter,
parentNodes: Node[],
childNode: Node,
childIsEmbed: boolean = false,
): string[] {
const parents: string[] = []
for (const [index, parentNode] of parentNodes.entries()) {
if (
index === parentNodes.length - 1 &&
parentNode.isTextblock &&
!parentNode.attrs.isAmgBlock &&
childNode.isText
(childNode.isText || childIsEmbed)
) {
// If the last node is a render-only text block then we don't need to emit it, the
// schema will take care of inserting it around the content for us
Expand Down Expand Up @@ -645,6 +646,23 @@ class TraverseState {
block.type.val,
block.isEmbed,
)
// Check if this inline embed can contain text - if so, push to stack
// so that subsequent text becomes children instead of siblings
const canContainText =
content.isInline &&
content.contentMatch.matchType(this.adapter.schema.nodes.text) != null
if (canContainText) {
// Use findWrapping to get necessary wrappers (e.g., paragraph)
const wrapping = this.currentMatch.findWrapping(content)
if (wrapping) {
for (let i = 0; i < wrapping.length; i++) {
yield this.pushNode(wrapping[i], null, "render-only")
}
}
yield blockEvent(this.adapter, block)
yield this.pushNode(content, null, "explicit")
return
}
const wrapping = this.currentMatch.findWrapping(content)
if (wrapping) {
for (let i = 0; i < wrapping.length; i++) {
Expand Down
24 changes: 23 additions & 1 deletion test/inlineNodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SchemaAdapter } from "../src/schema.js"
import * as am from "@automerge/automerge"
import { assertPmDocsEqual } from "./utils.js"

describe.only("when handling inline nodes", () => {
describe("when handling inline nodes", () => {
const adapter = new SchemaAdapter({
nodes: {
doc: {
Expand Down Expand Up @@ -41,6 +41,15 @@ describe.only("when handling inline nodes", () => {
return ["math-inline", 0]
},
},
math_inline_embed: {
automerge: {
block: "math-inline-embed",
isEmbed: true,
},
content: "text*",
group: "inline",
inline: true,
},
unknownBlock: {
automerge: {
unknownBlock: true,
Expand Down Expand Up @@ -102,6 +111,19 @@ describe.only("when handling inline nodes", () => {
actual: pmDoc,
})
})

it("should place text inside inline embed nodes when isEmbed is true", () => {
const original = schema.node("doc", null, [
schema.node("paragraph", { isAmgBlock: false }, [
schema.node("math_inline_embed", { isAmgBlock: true }, [
schema.text("x = y"),
]),
]),
])
const spans = pmNodeToSpans(adapter, original)
const roundTripped = pmDocFromSpans(adapter, spans)
assertPmDocsEqual({ expected: original, actual: roundTripped })
})
})

describe("when generating spans from a document", () => {
Expand Down