@milkdown/transformer
Advanced tools
Comparing version 7.5.0 to 7.5.8
@@ -1,15 +0,11 @@ | ||
var H = (p, h, n) => { | ||
if (!h.has(p)) | ||
throw TypeError("Cannot " + n); | ||
var H = (p) => { | ||
throw TypeError(p); | ||
}; | ||
var i = (p, h, n) => (H(p, h, "read from private field"), n ? n.call(p) : h.get(p)), u = (p, h, n) => { | ||
if (h.has(p)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
h instanceof WeakSet ? h.add(p) : h.set(p, n); | ||
}, o = (p, h, n, t) => (H(p, h, "write to private field"), t ? t.call(p, n) : h.set(p, n), n); | ||
import { stackOverFlow as Q, parserMatchError as X, createNodeInParserFail as Y, serializerMatchError as Z } from "@milkdown/exception"; | ||
var K = (p, h, n) => h.has(p) || H("Cannot " + n); | ||
var i = (p, h, n) => (K(p, h, "read from private field"), n ? n.call(p) : h.get(p)), u = (p, h, n) => h.has(p) ? H("Cannot add the same private member more than once") : h instanceof WeakSet ? h.add(p) : h.set(p, n), o = (p, h, n, t) => (K(p, h, "write to private field"), t ? t.call(p, n) : h.set(p, n), n); | ||
import { stackOverFlow as U, parserMatchError as Y, createNodeInParserFail as Z, serializerMatchError as _ } from "@milkdown/exception"; | ||
import { Mark as J } from "@milkdown/prose/model"; | ||
class U { | ||
class V { | ||
} | ||
class V { | ||
class X { | ||
constructor() { | ||
@@ -23,4 +19,3 @@ this.elements = [], this.size = () => this.elements.length, this.top = () => this.elements.at(-1), this.push = (h) => { | ||
const h = this.elements.pop(); | ||
if (!h) | ||
throw Q(); | ||
if (!h) throw U(); | ||
return h; | ||
@@ -30,3 +25,3 @@ }; | ||
} | ||
class B extends U { | ||
class B extends V { | ||
constructor(h, n, t) { | ||
@@ -46,13 +41,13 @@ super(), this.type = h, this.content = n, this.attrs = t; | ||
var d, M, O, T, F, w, g; | ||
const $ = class $ extends V { | ||
const $ = class $ extends X { | ||
/// @internal | ||
constructor(n) { | ||
super(); | ||
u(this, d, void 0); | ||
u(this, M, void 0); | ||
u(this, O, void 0); | ||
u(this, T, void 0); | ||
u(this, F, void 0); | ||
u(this, w, void 0); | ||
u(this, g, void 0); | ||
u(this, d); | ||
u(this, M); | ||
u(this, O); | ||
u(this, T); | ||
u(this, F); | ||
u(this, w); | ||
u(this, g); | ||
o(this, d, J.none), o(this, M, (t) => t.isText), o(this, O, (t, s) => { | ||
@@ -62,5 +57,7 @@ if (i(this, M).call(this, t) && i(this, M).call(this, s) && J.sameSet(t.marks, s.marks)) | ||
}), o(this, T, (t) => { | ||
const s = Object.values({ ...this.schema.nodes, ...this.schema.marks }).find((e) => e.spec.parseMarkdown.match(t)); | ||
if (!s) | ||
throw X(t); | ||
const s = Object.values({ | ||
...this.schema.nodes, | ||
...this.schema.marks | ||
}).find((e) => e.spec.parseMarkdown.match(t)); | ||
if (!s) throw Y(t); | ||
return s; | ||
@@ -76,4 +73,3 @@ }), o(this, F, (t) => { | ||
const r = t.createAndFill(s, e, i(this, d)); | ||
if (!r) | ||
throw Y(t, s, e); | ||
if (!r) throw Z(t, s, e); | ||
return this.push(r), r; | ||
@@ -85,4 +81,3 @@ }), this.addNode = (t, s, e) => (i(this, g).call(this, t, s, e), this), this.openMark = (t, s) => { | ||
const s = this.top(); | ||
if (!s) | ||
throw Q(); | ||
if (!s) throw U(); | ||
const e = s.pop(), r = this.schema.text(t, i(this, d)); | ||
@@ -100,3 +95,6 @@ if (!e) | ||
}, this.next = (t = []) => ([t].flat().forEach((s) => i(this, F).call(this, s)), this), this.toDoc = () => this.build(), this.run = (t, s) => { | ||
const e = t.runSync(t.parse(s), s); | ||
const e = t.runSync( | ||
t.parse(s), | ||
s | ||
); | ||
return this.next(e), this; | ||
@@ -110,4 +108,4 @@ }, this.schema = n; | ||
}; | ||
let K = $; | ||
const q = class q extends U { | ||
let L = $; | ||
const q = class q extends V { | ||
constructor(h, n, t, s = {}) { | ||
@@ -124,25 +122,27 @@ super(), this.type = h, this.children = n, this.value = t, this.props = s, this.push = (e, ...r) => { | ||
let W = q; | ||
const _ = (p) => Object.prototype.hasOwnProperty.call(p, "size"); | ||
const tt = (p) => Object.prototype.hasOwnProperty.call(p, "size"); | ||
var v, P, A, S, I, j, b, C, R, x, N, D, E; | ||
const z = class z extends V { | ||
const z = class z extends X { | ||
/// @internal | ||
constructor(n) { | ||
super(); | ||
u(this, v, void 0); | ||
u(this, P, void 0); | ||
u(this, A, void 0); | ||
u(this, S, void 0); | ||
u(this, I, void 0); | ||
u(this, j, void 0); | ||
u(this, b, void 0); | ||
u(this, C, void 0); | ||
u(this, R, void 0); | ||
u(this, x, void 0); | ||
u(this, N, void 0); | ||
u(this, D, void 0); | ||
u(this, E, void 0); | ||
u(this, v); | ||
u(this, P); | ||
u(this, A); | ||
u(this, S); | ||
u(this, I); | ||
u(this, j); | ||
u(this, b); | ||
u(this, C); | ||
u(this, R); | ||
u(this, x); | ||
u(this, N); | ||
u(this, D); | ||
u(this, E); | ||
o(this, v, J.none), o(this, P, (t) => { | ||
const s = Object.values({ ...this.schema.nodes, ...this.schema.marks }).find((e) => e.spec.toMarkdown.match(t)); | ||
if (!s) | ||
throw Z(t.type); | ||
const s = Object.values({ | ||
...this.schema.nodes, | ||
...this.schema.marks | ||
}).find((e) => e.spec.toMarkdown.match(t)); | ||
if (!s) throw _(t.type); | ||
return s; | ||
@@ -154,15 +154,11 @@ }), o(this, A, (t) => i(this, P).call(this, t).spec.toMarkdown.runner(this, t)), o(this, S, (t, s) => i(this, P).call(this, t).spec.toMarkdown.runner(this, t, s)), o(this, I, (t) => { | ||
var f; | ||
if (t.type === s || ((f = t.children) == null ? void 0 : f.length) !== 1) | ||
return t; | ||
if (t.type === s || ((f = t.children) == null ? void 0 : f.length) !== 1) return t; | ||
const e = (y) => { | ||
var l; | ||
if (y.type === s) | ||
return y; | ||
if (((l = y.children) == null ? void 0 : l.length) !== 1) | ||
return null; | ||
if (y.type === s) return y; | ||
if (((l = y.children) == null ? void 0 : l.length) !== 1) return null; | ||
const [k] = y.children; | ||
return k ? e(k) : null; | ||
}, r = e(t); | ||
if (!r) | ||
return t; | ||
if (!r) return t; | ||
const a = r.children ? [...r.children] : void 0, c = { ...t, children: a }; | ||
@@ -173,4 +169,3 @@ return c.children = a, r.children = [c], r; | ||
return s && (t.children = s.reduce((e, r, a) => { | ||
if (a === 0) | ||
return [r]; | ||
if (a === 0) return [r]; | ||
const c = e.at(-1); | ||
@@ -227,3 +222,3 @@ if (c && c.isMark && r.isMark) { | ||
return t; | ||
}, this.next = (t) => _(t) ? (t.forEach((s) => { | ||
}, this.next = (t) => tt(t) ? (t.forEach((s) => { | ||
i(this, I).call(this, s); | ||
@@ -237,9 +232,9 @@ }), this) : (i(this, I).call(this, t), this), this.toString = (t) => t.stringify(this.build()), this.run = (t) => (this.next(t), this), this.schema = n; | ||
}; | ||
let L = z; | ||
let Q = z; | ||
export { | ||
K as ParserState, | ||
L as SerializerState, | ||
V as Stack, | ||
U as StackElement | ||
L as ParserState, | ||
Q as SerializerState, | ||
X as Stack, | ||
V as StackElement | ||
}; | ||
//# sourceMappingURL=index.es.js.map |
@@ -7,3 +7,3 @@ import type { MarkSpec, NodeSpec } from '@milkdown/prose/model'; | ||
export type Node = Parameters<Transformer>[0]; | ||
export type Root = Parameters<typeof remark['stringify']>[0]; | ||
export type Root = Parameters<(typeof remark)['stringify']>[0]; | ||
export type JSONValue = string | number | boolean | null | JSONValue[] | { | ||
@@ -10,0 +10,0 @@ [key: string]: JSONValue; |
{ | ||
"name": "@milkdown/transformer", | ||
"type": "module", | ||
"version": "7.5.0", | ||
"version": "7.5.8", | ||
"license": "MIT", | ||
@@ -38,6 +38,6 @@ "repository": { | ||
"unified": "^11.0.3", | ||
"@milkdown/exception": "7.5.0" | ||
"@milkdown/exception": "7.5.8" | ||
}, | ||
"devDependencies": { | ||
"@milkdown/prose": "7.5.0" | ||
"@milkdown/prose": "7.5.8" | ||
}, | ||
@@ -44,0 +44,0 @@ "nx": { |
@@ -5,3 +5,7 @@ import type { Attrs, Node, NodeType } from '@milkdown/prose/model' | ||
export class ParserStackElement extends StackElement<Node> { | ||
constructor(public type: NodeType, public content: Node[], public attrs?: Attrs) { | ||
constructor( | ||
public type: NodeType, | ||
public content: Node[], | ||
public attrs?: Attrs | ||
) { | ||
super() | ||
@@ -8,0 +12,0 @@ } |
@@ -6,19 +6,34 @@ import type { MarkType, NodeType, Schema } from '@milkdown/prose/model' | ||
const docNodeType = { | ||
createAndFill: vi.fn().mockImplementation((attrs, content, marks) => ({ name: 'docNode', content, attrs, marks })), | ||
createAndFill: vi.fn().mockImplementation((attrs, content, marks) => ({ | ||
name: 'docNode', | ||
content, | ||
attrs, | ||
marks, | ||
})), | ||
} as unknown as NodeType | ||
const paragraphNodeType = { | ||
createAndFill: vi.fn().mockImplementation((attrs, content, marks) => ({ name: 'paragraphNode', content, attrs, marks })), | ||
createAndFill: vi.fn().mockImplementation((attrs, content, marks) => ({ | ||
name: 'paragraphNode', | ||
content, | ||
attrs, | ||
marks, | ||
})), | ||
} as unknown as NodeType | ||
const blockquoteNodeType = { | ||
createAndFill: vi.fn().mockImplementation((attrs, content, marks) => ({ name: 'blockquoteNode', content, attrs, marks })), | ||
createAndFill: vi.fn().mockImplementation((attrs, content, marks) => ({ | ||
name: 'blockquoteNode', | ||
content, | ||
attrs, | ||
marks, | ||
})), | ||
} as unknown as NodeType | ||
const boldType = { | ||
create: vi.fn().mockImplementation(attrs => ({ | ||
create: vi.fn().mockImplementation((attrs) => ({ | ||
name: 'boldMark', | ||
attrs, | ||
addToSet: arr => arr.concat('bold'), | ||
removeFromSet: arr => arr.filter(x => x !== 'bold'), | ||
addToSet: (arr) => arr.concat('bold'), | ||
removeFromSet: (arr) => arr.filter((x) => x !== 'bold'), | ||
})), | ||
addToSet: arr => arr.concat('bold'), | ||
removeFromSet: arr => arr.filter(x => x !== 'bold'), | ||
addToSet: (arr) => arr.concat('bold'), | ||
removeFromSet: (arr) => arr.filter((x) => x !== 'bold'), | ||
} as unknown as MarkType | ||
@@ -31,3 +46,3 @@ | ||
parseMarkdown: { | ||
match: node => node.type === 'paragraphNode', | ||
match: (node) => node.type === 'paragraphNode', | ||
runner: (state, node) => { | ||
@@ -42,3 +57,3 @@ state.addText(node.value) | ||
parseMarkdown: { | ||
match: node => node.type === 'blockquoteNode', | ||
match: (node) => node.type === 'blockquoteNode', | ||
runner: (state, node) => { | ||
@@ -93,6 +108,3 @@ state.openNode(blockquoteNodeType) | ||
state.openNode(docNodeType) | ||
state | ||
.openMark(boldType) | ||
.addNode(paragraphNodeType) | ||
.closeMark(boldType) | ||
state.openMark(boldType).addNode(paragraphNodeType).closeMark(boldType) | ||
@@ -116,3 +128,3 @@ expect(state.top()).toMatchObject({ | ||
.addText('The lunatic is on the grass.\n') | ||
.addText('I\'ll see you on the dark side of the moon.') | ||
.addText("I'll see you on the dark side of the moon.") | ||
.closeNode() | ||
@@ -126,3 +138,3 @@ | ||
{ | ||
text: 'The lunatic is on the grass.\nI\'ll see you on the dark side of the moon.', | ||
text: "The lunatic is on the grass.\nI'll see you on the dark side of the moon.", | ||
}, | ||
@@ -143,3 +155,3 @@ ], | ||
.addText('The lunatic is on the grass.\n') | ||
.addText('I\'ll see you on the dark side of the moon.') | ||
.addText("I'll see you on the dark side of the moon.") | ||
.closeMark(boldType) | ||
@@ -154,3 +166,3 @@ .closeNode() | ||
{ | ||
text: 'The lunatic is on the grass.\nI\'ll see you on the dark side of the moon.', | ||
text: "The lunatic is on the grass.\nI'll see you on the dark side of the moon.", | ||
}, | ||
@@ -172,3 +184,3 @@ ], | ||
.closeMark(boldType) | ||
.addText('I\'ll see you on the dark side of the moon.') | ||
.addText("I'll see you on the dark side of the moon.") | ||
.closeNode() | ||
@@ -185,3 +197,3 @@ | ||
{ | ||
text: 'I\'ll see you on the dark side of the moon.', | ||
text: "I'll see you on the dark side of the moon.", | ||
}, | ||
@@ -188,0 +200,0 @@ ], |
@@ -1,5 +0,20 @@ | ||
import type { Attrs, MarkType, Node, NodeType, Schema } from '@milkdown/prose/model' | ||
import { createNodeInParserFail, parserMatchError, stackOverFlow } from '@milkdown/exception' | ||
import type { | ||
Attrs, | ||
MarkType, | ||
Node, | ||
NodeType, | ||
Schema, | ||
} from '@milkdown/prose/model' | ||
import { | ||
createNodeInParserFail, | ||
parserMatchError, | ||
stackOverFlow, | ||
} from '@milkdown/exception' | ||
import { Mark } from '@milkdown/prose/model' | ||
import type { MarkSchema, MarkdownNode, NodeSchema, RemarkParser } from '../utility' | ||
import type { | ||
MarkSchema, | ||
MarkdownNode, | ||
NodeSchema, | ||
RemarkParser, | ||
} from '../utility' | ||
import { Stack } from '../utility' | ||
@@ -51,10 +66,11 @@ | ||
#matchTarget = (node: MarkdownNode): NodeType | MarkType => { | ||
const result = Object.values({ ...this.schema.nodes, ...this.schema.marks }) | ||
.find((x): x is (NodeType | MarkType) => { | ||
const spec = x.spec as NodeSchema | MarkSchema | ||
return spec.parseMarkdown.match(node) | ||
}) | ||
const result = Object.values({ | ||
...this.schema.nodes, | ||
...this.schema.marks, | ||
}).find((x): x is NodeType | MarkType => { | ||
const spec = x.spec as NodeSchema | MarkSchema | ||
return spec.parseMarkdown.match(node) | ||
}) | ||
if (!result) | ||
throw parserMatchError(node) | ||
if (!result) throw parserMatchError(node) | ||
@@ -102,6 +118,9 @@ return result | ||
/// @internal | ||
#addNodeAndPush = (nodeType: NodeType, attrs?: Attrs, content?: Node[]): Node => { | ||
#addNodeAndPush = ( | ||
nodeType: NodeType, | ||
attrs?: Attrs, | ||
content?: Node[] | ||
): Node => { | ||
const node = nodeType.createAndFill(attrs, content, this.#marks) | ||
if (!node) | ||
throw createNodeInParserFail(nodeType, attrs, content) | ||
if (!node) throw createNodeInParserFail(nodeType, attrs, content) | ||
@@ -136,4 +155,3 @@ this.push(node) | ||
const topElement = this.top() | ||
if (!topElement) | ||
throw stackOverFlow() | ||
if (!topElement) throw stackOverFlow() | ||
@@ -161,4 +179,3 @@ const prevNode = topElement.pop() | ||
do | ||
doc = this.#closeNodeAndPush() | ||
do doc = this.#closeNodeAndPush() | ||
while (this.size()) | ||
@@ -172,3 +189,3 @@ | ||
next = (nodes: MarkdownNode | MarkdownNode[] = []) => { | ||
[nodes].flat().forEach(node => this.#runNode(node)) | ||
;[nodes].flat().forEach((node) => this.#runNode(node)) | ||
return this | ||
@@ -182,3 +199,6 @@ } | ||
run = (remark: RemarkParser, markdown: string) => { | ||
const tree = remark.runSync(remark.parse(markdown), markdown) as MarkdownNode | ||
const tree = remark.runSync( | ||
remark.parse(markdown), | ||
markdown | ||
) as MarkdownNode | ||
this.next(tree) | ||
@@ -185,0 +205,0 @@ |
@@ -10,3 +10,3 @@ import type { MarkdownNode } from '..' | ||
public value?: string, | ||
public props: JSONRecord = {}, | ||
public props: JSONRecord = {} | ||
) { | ||
@@ -20,8 +20,7 @@ super() | ||
value?: string, | ||
props: JSONRecord = {}, | ||
props: JSONRecord = {} | ||
) => new SerializerStackElement(type, children, value, props) | ||
push = (node: MarkdownNode, ...rest: MarkdownNode[]) => { | ||
if (!this.children) | ||
this.children = [] | ||
if (!this.children) this.children = [] | ||
@@ -28,0 +27,0 @@ this.children.push(node, ...rest) |
@@ -6,6 +6,6 @@ import type { Mark, Schema } from '@milkdown/prose/model' | ||
const boldMark = { | ||
isInSet: arr => arr.includes('bold'), | ||
addToSet: arr => arr.concat('bold'), | ||
isInSet: (arr) => arr.includes('bold'), | ||
addToSet: (arr) => arr.concat('bold'), | ||
type: { | ||
removeFromSet: arr => arr.filter(x => x !== 'bold'), | ||
removeFromSet: (arr) => arr.filter((x) => x !== 'bold'), | ||
}, | ||
@@ -19,3 +19,3 @@ } as unknown as Mark | ||
toMarkdown: { | ||
match: node => node.type === 'paragraph', | ||
match: (node) => node.type === 'paragraph', | ||
runner: (state, node) => { | ||
@@ -30,3 +30,3 @@ state.addNode('text', [], node.value) | ||
toMarkdown: { | ||
match: node => node.type === 'blockquote', | ||
match: (node) => node.type === 'blockquote', | ||
runner: (state, node) => { | ||
@@ -33,0 +33,0 @@ state.openNode('blockquote') |
import { serializerMatchError } from '@milkdown/exception' | ||
import type { Fragment, MarkType, Node, NodeType, Schema } from '@milkdown/prose/model' | ||
import type { | ||
Fragment, | ||
MarkType, | ||
Node, | ||
NodeType, | ||
Schema, | ||
} from '@milkdown/prose/model' | ||
import { Mark } from '@milkdown/prose/model' | ||
import type { JSONRecord, MarkSchema, MarkdownNode, NodeSchema, RemarkParser, Root } from '../utility' | ||
import type { | ||
JSONRecord, | ||
MarkSchema, | ||
MarkdownNode, | ||
NodeSchema, | ||
RemarkParser, | ||
Root, | ||
} from '../utility' | ||
import { Stack } from '../utility' | ||
@@ -10,7 +23,11 @@ import { SerializerStackElement } from './stack-element' | ||
const isFragment = (x: Node | Fragment): x is Fragment => Object.prototype.hasOwnProperty.call(x, 'size') | ||
const isFragment = (x: Node | Fragment): x is Fragment => | ||
Object.prototype.hasOwnProperty.call(x, 'size') | ||
/// State for serializer. | ||
/// Transform prosemirror state into remark AST. | ||
export class SerializerState extends Stack<MarkdownNode, SerializerStackElement> { | ||
export class SerializerState extends Stack< | ||
MarkdownNode, | ||
SerializerStackElement | ||
> { | ||
/// @internal | ||
@@ -43,10 +60,11 @@ #marks: readonly Mark[] = Mark.none | ||
#matchTarget = (node: Node | Mark): NodeType | MarkType => { | ||
const result = Object.values({ ...this.schema.nodes, ...this.schema.marks }) | ||
.find((x): x is (NodeType | MarkType) => { | ||
const spec = x.spec as NodeSchema | MarkSchema | ||
return spec.toMarkdown.match(node as Node & Mark) | ||
}) | ||
const result = Object.values({ | ||
...this.schema.nodes, | ||
...this.schema.marks, | ||
}).find((x): x is NodeType | MarkType => { | ||
const spec = x.spec as NodeSchema | MarkSchema | ||
return spec.toMarkdown.match(node as Node & Mark) | ||
}) | ||
if (!result) | ||
throw serializerMatchError(node.type) | ||
if (!result) throw serializerMatchError(node.type) | ||
@@ -75,7 +93,6 @@ return result | ||
const tmp = [...marks].sort((a, b) => getPriority(a) - getPriority(b)) | ||
const unPreventNext = tmp.every(mark => !this.#runProseMark(mark, node)) | ||
if (unPreventNext) | ||
this.#runProseNode(node) | ||
const unPreventNext = tmp.every((mark) => !this.#runProseMark(mark, node)) | ||
if (unPreventNext) this.#runProseNode(node) | ||
marks.forEach(mark => this.#closeMark(mark)) | ||
marks.forEach((mark) => this.#closeMark(mark)) | ||
} | ||
@@ -85,18 +102,13 @@ | ||
#searchType = (child: MarkdownNode, type: string): MarkdownNode => { | ||
if (child.type === type) | ||
return child | ||
if (child.type === type) return child | ||
if (child.children?.length !== 1) | ||
return child | ||
if (child.children?.length !== 1) return child | ||
const searchNode = (node: MarkdownNode): MarkdownNode | null => { | ||
if (node.type === type) | ||
return node | ||
if (node.type === type) return node | ||
if (node.children?.length !== 1) | ||
return null | ||
if (node.children?.length !== 1) return null | ||
const [firstChild] = node.children | ||
if (!firstChild) | ||
return null | ||
if (!firstChild) return null | ||
@@ -108,4 +120,3 @@ return searchNode(firstChild) | ||
if (!target) | ||
return child | ||
if (!target) return child | ||
@@ -123,8 +134,6 @@ const tmp = target.children ? [...target.children] : undefined | ||
const { children } = node | ||
if (!children) | ||
return node | ||
if (!children) return node | ||
node.children = children.reduce((nextChildren, child, index) => { | ||
if (index === 0) | ||
return [child] | ||
if (index === 0) return [child] | ||
@@ -137,6 +146,6 @@ const last = nextChildren.at(-1) | ||
if ( | ||
child.type === last.type | ||
&& currChildren | ||
&& prevChildren | ||
&& JSON.stringify(currRest) === JSON.stringify(prevRest) | ||
child.type === last.type && | ||
currChildren && | ||
prevChildren && | ||
JSON.stringify(currRest) === JSON.stringify(prevRest) | ||
) { | ||
@@ -165,7 +174,5 @@ const next = { | ||
if (element.children) | ||
node.children = element.children | ||
if (element.children) node.children = element.children | ||
if (element.value) | ||
node.value = element.value | ||
if (element.value) node.value = element.value | ||
@@ -182,3 +189,6 @@ return node | ||
#moveSpaces = (element: SerializerStackElement, onPush: () => MarkdownNode) => { | ||
#moveSpaces = ( | ||
element: SerializerStackElement, | ||
onPush: () => MarkdownNode | ||
) => { | ||
let startSpaces = '' | ||
@@ -190,8 +200,6 @@ let endSpaces = '' | ||
const findIndex = (node: MarkdownNode[]) => { | ||
if (!node) | ||
return | ||
if (!node) return | ||
node.forEach((child, index) => { | ||
if (child.type === 'text' && child.value) { | ||
if (first < 0) | ||
first = index | ||
if (first < 0) first = index | ||
@@ -205,4 +213,8 @@ last = index | ||
findIndex(children) | ||
const lastChild = children?.[last] as MarkdownNode & { value: string } | undefined | ||
const firstChild = children?.[first] as MarkdownNode & { value: string } | undefined | ||
const lastChild = children?.[last] as | ||
| (MarkdownNode & { value: string }) | ||
| undefined | ||
const firstChild = children?.[first] as | ||
| (MarkdownNode & { value: string }) | ||
| undefined | ||
if (lastChild && lastChild.value.endsWith(' ')) { | ||
@@ -218,9 +230,7 @@ endSpaces = lastChild.value.match(/ +$/)![0] | ||
if (startSpaces.length) | ||
this.#addNodeAndPush('text', undefined, startSpaces) | ||
if (startSpaces.length) this.#addNodeAndPush('text', undefined, startSpaces) | ||
const result = onPush() | ||
if (endSpaces.length) | ||
this.#addNodeAndPush('text', undefined, endSpaces) | ||
if (endSpaces.length) this.#addNodeAndPush('text', undefined, endSpaces) | ||
@@ -234,6 +244,11 @@ return result | ||
const onPush = () => this.#addNodeAndPush(element.type, element.children, element.value, element.props) | ||
const onPush = () => | ||
this.#addNodeAndPush( | ||
element.type, | ||
element.children, | ||
element.value, | ||
element.props | ||
) | ||
if (trim) | ||
return this.#moveSpaces(element, onPush) | ||
if (trim) return this.#moveSpaces(element, onPush) | ||
@@ -250,5 +265,12 @@ return onPush() | ||
/// @internal | ||
#addNodeAndPush = (type: string, children?: MarkdownNode[], value?: string, props?: JSONRecord): MarkdownNode => { | ||
#addNodeAndPush = ( | ||
type: string, | ||
children?: MarkdownNode[], | ||
value?: string, | ||
props?: JSONRecord | ||
): MarkdownNode => { | ||
const element = SerializerStackElement.create(type, children, value, props) | ||
const node: MarkdownNode = this.#maybeMergeChildren(this.#createMarkdownNode(element)) | ||
const node: MarkdownNode = this.#maybeMergeChildren( | ||
this.#createMarkdownNode(element) | ||
) | ||
this.push(node) | ||
@@ -259,3 +281,8 @@ return node | ||
/// Add a node into current node. | ||
addNode = (type: string, children?: MarkdownNode[], value?: string, props?: JSONRecord) => { | ||
addNode = ( | ||
type: string, | ||
children?: MarkdownNode[], | ||
value?: string, | ||
props?: JSONRecord | ||
) => { | ||
this.#addNodeAndPush(type, children, value, props) | ||
@@ -266,7 +293,11 @@ return this | ||
/// @internal | ||
#openMark = (mark: Mark, type: string, value?: string, props?: JSONRecord) => { | ||
#openMark = ( | ||
mark: Mark, | ||
type: string, | ||
value?: string, | ||
props?: JSONRecord | ||
) => { | ||
const isIn = mark.isInSet(this.#marks) | ||
if (isIn) | ||
return this | ||
if (isIn) return this | ||
@@ -281,4 +312,3 @@ this.#marks = mark.addToSet(this.#marks) | ||
if (!isIn) | ||
return | ||
if (!isIn) return | ||
@@ -307,4 +337,3 @@ this.#marks = mark.type.removeFromSet(this.#marks) | ||
let doc: MarkdownNode | null = null | ||
do | ||
doc = this.#closeNodeAndPush() | ||
do doc = this.#closeNodeAndPush() | ||
while (this.size()) | ||
@@ -329,3 +358,4 @@ | ||
/// Use a remark parser to serialize current AST stored. | ||
override toString = (remark: RemarkParser): string => remark.stringify(this.build() as Root) | ||
override toString = (remark: RemarkParser): string => | ||
remark.stringify(this.build() as Root) | ||
@@ -332,0 +362,0 @@ /// Transform a prosemirror node tree into remark AST. |
@@ -44,4 +44,3 @@ import { stackOverFlow } from '@milkdown/exception' | ||
const el = this.elements.pop() | ||
if (!el) | ||
throw stackOverFlow() | ||
if (!el) throw stackOverFlow() | ||
@@ -48,0 +47,0 @@ return el |
@@ -5,3 +5,6 @@ import type { MarkSpec, NodeSpec } from '@milkdown/prose/model' | ||
import type { MarkParserSpec, NodeParserSpec } from '../parser/types' | ||
import type { MarkSerializerSpec, NodeSerializerSpec } from '../serializer/types' | ||
import type { | ||
MarkSerializerSpec, | ||
NodeSerializerSpec, | ||
} from '../serializer/types' | ||
@@ -12,6 +15,12 @@ /// @internal | ||
/// @internal | ||
export type Root = Parameters<typeof remark['stringify']>[0] | ||
export type Root = Parameters<(typeof remark)['stringify']>[0] | ||
/// @internal | ||
export type JSONValue = string | number | boolean | null | JSONValue[] | { [key: string]: JSONValue } | ||
export type JSONValue = | ||
| string | ||
| number | ||
| boolean | ||
| null | ||
| JSONValue[] | ||
| { [key: string]: JSONValue } | ||
@@ -34,3 +43,6 @@ /// @internal | ||
/// The universal type of a node in [mdast](https://github.com/syntax-tree/mdast). | ||
export type MarkdownNode = Node & { children?: MarkdownNode[], [x: string]: unknown } | ||
export type MarkdownNode = Node & { | ||
children?: MarkdownNode[] | ||
[x: string]: unknown | ||
} | ||
@@ -37,0 +49,0 @@ /// Schema spec for node. It is a super set of [NodeSpec](https://prosemirror.net/docs/ref/#model.NodeSpec). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
87631
1499
+ Added@milkdown/exception@7.5.8(transitive)
- Removed@milkdown/exception@7.5.0(transitive)
Updated@milkdown/exception@7.5.8