【发布时间】:2021-09-05 05:36:19
【问题描述】:
我想将用 Markdown 编写的长文章迁移到富文本字段。除了桌子,我什么都能处理。 In the documentation it says:
将内容迁移到一个链接条目中,该条目只有一个带有支持内容的 Markdown 字段,这类似于步骤 8 中执行的操作
但我无法让它工作。在提到的第 8 步中,没有创建新条目,仅链接现有条目。我还没有弄清楚如何动态创建一个类型为表的新条目(它只是一个带有名称内容的 Markdown 类型的字段)并在富文本内容中引用它。 derivedLinkedEntries() 想要将引用放入特定字段,但这是内联的,可以存在多次。
我可以在富文本字段中手动创建一个表并内联对内容的引用,但我想使用迁移脚本创建它,但不知道如何。
这些是我的内容类型:
- Article with old content field and new one
- Table element to be inlined
- Inline Table element in rich text field
这就是我在控制台上得到的一张桌子:
{
type: 'table',
align: [ null, 'center', 'right' ],
children: [
{ type: 'tableRow', children: [Array], position: [Position] },
{ type: 'tableRow', children: [Array], position: [Position] }
],
position: Position {
start: { line: 14, column: 1, offset: 970 },
end: { line: 25, column: 58, offset: 1660 },
indent: [
1, 1
]
}
}
这是我到目前为止的代码(为简洁起见):
const {richTextFromMarkdown} = require('@contentful/rich-text-from-markdown')
module.exports = function(migration) {
migration.transformEntries({
contentType: 'article',
from: ['content'],
to: ['contentV2'],
transformEntryForLocale: async function(fromFields, currentLocale)
{
let copy = fromFields.content[currentLocale]
const content = await richTextFromMarkdown(copy,
(node) => {
let ret = null
let didSomething = true
node.deriveLinkedEntries()
switch (node.type) {
case 'image':
// ...
break;
case 'html':
// ...
break;
default:
didSomething = false
}
if (false === didSomething) {
console.log(node)
}
return ret
}
)
return {
contentV2: {
nodeType: 'document',
content: content.content,
data: {}
},
}
}
})
}
【问题讨论】:
标签: typescript markdown data-migration contentful richtext