我需要使用ProseMirror-Markdown文本编辑器在我当前正在开发的Angular应用程序中获得表支持。我有点卡住。由于该项目中的现有功能(现在已与ProseMirror-Markdown结合使用),我决定使用prosemirror-markdown及其MarkdownSerializer。
// heading example
get schema() {
return {
toMarkdown: (state, node) => {
state.write(`${state.repeat('#', node.attrs.level)} `)
state.renderInline(node)
state.closeBlock(node)
},
}
}
不幸的是,我遇到了与TipTap的贡献者相同的问题 GitHub issue with prosemirror-markdown & GFM tables
无官方表支持使用prosemirror-markdown的缺点 是表不支持(也不会)。我可以写 一部分,但那意味着我们必须放弃一些高级功能 表的功能,如多行,嵌套表, 表等中的列表。也许我们应该提供一个Table节点 是否支持markdown和不支持markdown的ExtendedTable?
之后,我尝试实施一个 parseMarkdown()函数,这更加复杂。有 在prosemirror-markdown中使用Markdown-it的MarkdownParser 在引擎盖下。很好,但这意味着我们受此限制 解析器。我一直认为那里有Markdown解析器 扩展非常简单的api,但我想我错了。 :(
我正在使用MarkdownIt进行解析和序列化,以下是相关的package.json
条目:
{...,
"markdown-it": "^9.1.0",
"markdown-it-table": "^2.0.4",
"prosemirror-keymap": "^1.0.0",
"prosemirror-markdown": "^1.3.1",
"prosemirror-model": "^1.0.0",
"prosemirror-state": "^1.0.0",
"prosemirror-transform": "^1.0.0",
"prosemirror-view": "^1.0.10",
"prosemirror-commands": "^1.0.0",
"prosemirror-example-setup": "^1.0.0",
"prosemirror-menu": "^1.0.0",
"prosemirror-schema-basic": "^1.0.0",
...}
我在解析器和markdown-table中添加了一些自定义标记,这些标记可以在较高级别正确显示:
但是,即使我清除了所有未解决的令牌错误,仍在编辑器视图中得到了部分渲染的表:
这些是在初始化时已添加到解析器的令牌:
+ table: {block: "table"},
+ thead: {block: "table_row"},
+ tr: {block: "table_row"},
+ td: {block: "table_cell"},
+ th: {block: "table_cell"},
+ tbody: {block: "tbody"},
这些是在架构中定义的表节点:
// Helper for creating a schema that supports tables.
function getCellAttrs(dom, extraAttrs) {
let widthAttr = dom.getAttribute("data-colwidth")
let widths = widthAttr && /^\d+(,\d+)*$/.test(widthAttr) ? widthAttr.split(",").map(s => Number(s)) : null
let colspan = Number(dom.getAttribute("colspan") || 1)
let result = {
colspan,
rowspan: Number(dom.getAttribute("rowspan") || 1),
colwidth: widths && widths.length == colspan ? widths : null
}
for (let prop in extraAttrs) {
let getter = extraAttrs[prop].getFromDOM
let value = getter && getter(dom)
if (value != null) result[prop] = value
}
return result
}
function setCellAttrs(node, extraAttrs) {
let attrs = {}
if (node.attrs.colspan != 1) attrs.colspan = node.attrs.colspan
if (node.attrs.rowspan != 1) attrs.rowspan = node.attrs.rowspan
if (node.attrs.colwidth)
attrs["data-colwidth"] = node.attrs.colwidth.join(",")
for (let prop in extraAttrs) {
let setter = extraAttrs[prop].setDOMAttr
if (setter) setter(node.attrs[prop], attrs)
}
return attrs
}
export function tableNodes(options) {
let extraAttrs = options.cellAttributes || {}
let cellAttrs = {
colspan: {default: 1},
rowspan: {default: 1},
colwidth: {default: null}
}
for (let prop in extraAttrs)
cellAttrs[prop] = {default: extraAttrs[prop].default}
return {
table: {
content: "table_row+",
tableRole: "table",
isolating: true,
group: options.tableGroup,
parseDOM: [{tag: "table"}],
toDOM() { return ["table", ["tbody", 0]] }
},
table_row: {
content: "(table_cell | table_header)*",
tableRole: "row",
parseDOM: [{tag: "tr"}],
toDOM() { return ["tr", 0] }
},
cell: {
content: options.cellContent,
attrs: cellAttrs,
tableRole: "cell",
isolating: true,
parseDOM: [{tag: "td", getAttrs: dom => getCellAttrs(dom, extraAttrs)}],
toDOM(node) { return ["td", setCellAttrs(node, extraAttrs), 0] }
},
table_header: {
content: options.cellContent,
attrs: cellAttrs,
tableRole: "header_cell",
isolating: true,
parseDOM: [{tag: "th", getAttrs: dom => getCellAttrs(dom, extraAttrs)}],
toDOM(node) { return ["th", setCellAttrs(node, extraAttrs), 0] }
}
}
}
export function tableNodeTypes(schema) {
let result = schema.cached.tableNodeTypes
if (!result) {
result = schema.cached.tableNodeTypes = {}
for (let name in schema.nodes) {
let type = schema.nodes[name], role = type.spec.tableRole
if (role) result[role] = type
}
}
return result
}
任何人都对此有经验或可以提出另一种方法?任何帮助,将不胜感激!