使用 javascript / node js 将一种形式的 json 转换为另一种形式的 json 对象
converting one form of json into another form of json object using javascript / node js
此处 data1
和 content
的顺序每次都会更改,这就是我使用 switch 语句的原因
我是运行一个函数,通过这样传递值
parser(values[key]);
这个值[键]) 包含波纹管对象
以下对象/数据结构
{
"nodeType": "document",
"data": {},
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "dummy testing bold",
"marks": [
{
"type": "bold"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "jsdbhcjbjsdz'c;s bold and italic",
"marks": [
{
"type": "bold"
},
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "underline bold and italic",
"marks": [
{
"type": "bold"
},
{
"type": "italic"
},
{
"type": "underline"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "ldwc;s,l;,aws italic",
"marks": [
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "hr",
"content": [],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "testing",
"marks": [
{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "unordered-list",
"content": [
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "hello ji",
"marks": [
{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "nahi ji",
"marks": [
{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "testing ji",
"marks": [
{
"type": "underline"
},
{
"type": "italic"
},
{
"type": "bold"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "ordered-list",
"content": [
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "testing",
"marks": [
{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "newh",
"marks": [
{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "blockquote",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "blockquotes",
"marks": [
{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "blockquotes",
"marks": [
{
"type": "bold"
},
{
"type": "code"
},
{
"type": "underline"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-1",
"content": [
{
"nodeType": "text",
"value": "Heading 1",
"marks": [
{
"type": "bold"
},
{
"type": "underline"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-2",
"content": [
{
"nodeType": "text",
"value": "heading 2",
"marks": [
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-3",
"content": [
{
"nodeType": "text",
"value": "heading 3",
"marks": [
{
"type": "italic"
},
{
"type": "underline"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-4",
"content": [
{
"nodeType": "text",
"value": "heading 4",
"marks": [
{
"type": "bold"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-5",
"content": [
{
"nodeType": "text",
"value": "heading 5",
"marks": [
{
"type": "underline"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-6",
"content": [
{
"nodeType": "text",
"value": "heading 6",
"marks": [
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "hr",
"content": [],
"data": {}
},
{
"nodeType": "embedded-entry-block",
"content": [],
"data": {
"target": {
"sys": {
"id": "651dYO8d4oE0MVJlVePWr6",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "hyperlink",
"content": [
{
"nodeType": "text",
"value": "Google",
"marks": [],
"data": {}
}
],
"data": {
"uri": "www.google.com"
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "entry-hyperlink",
"content": [
{
"nodeType": "text",
"value": "Entry",
"marks": [],
"data": {}
}
],
"data": {
"target": {
"sys": {
"id": "66rzYr2BpWL1VTBHdLTdSW",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "asset-hyperlink",
"content": [
{
"nodeType": "text",
"value": "Asset",
"marks": [],
"data": {}
}
],
"data": {
"target": {
"sys": {
"id": "2FfbMwKRZ2QK52MsbYxZbb",
"type": "Link",
"linkType": "Asset"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "hi code here",
"marks": [
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "embedded-entry-block",
"content": [],
"data": {
"target": {
"sys": {
"id": "4zu7f2YP4cXqR6aeeMxEt8",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "embedded-entry-inline",
"content": [],
"data": {
"target": {
"sys": {
"id": "4zu7f2YP4cXqR6aeeMxEt8",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "embedded-entry-block",
"content": [],
"data": {
"target": {
"sys": {
"id": "2eAmIIuG4xkLvatkU3RUSy",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "embedded-entry-inline",
"content": [],
"data": {
"target": {
"sys": {
"id": "49VJ3thpTULacWCcnjbOXz",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
}
]
}
...应转换为另一种自定义结构,如下所示...
[
{
"type": "doc",
"attrs": { "dirty": true },
"children": [
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "text": "dummy testing bold", "bold": true }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [
{
"text": "jsdbhcjbjsdz'c;s bold and italic",
"bold": true,
"italic": true
}
]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [
{
"text": "underline bold and italic",
"bold": true,
"italic": true,
"underline": true
}
]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "text": "idwc:s,l:.aws italic", "italic": true }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "italic": true, "text": "" }]
},
{
"type": "hr",
"children": [{ "text": "" }],
"attrs": { "dirty": true }
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "text": "" }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "text": "testing", "italic": true }]
},
{
"type": "ul",
"children": [
{
"type": "li",
"attrs": { "dirty": true },
"children": [{ "italic": true, "text": "hello ji" }]
},
{
"type": "li",
"attrs": { "dirty": true },
"children": [{ "italic": true, "text": "nahi ji" }]
},
{
"type": "li",
"attrs": { "dirty": true },
"children": [
{
"text": "testing ji",
"italic": true,
"bold": true,
"underline": true
}
]
}
],
"id": "6c180306f6204829999b78e8dd482d33",
"attrs": { "dirty": true }
},
{
"type": "ol",
"children": [
{
"type": "li",
"children": [{ "text": "testing", "italic": true }],
"attrs": { "dirty": true }
},
{
"type": "li",
"attrs": { "dirty": true },
"children": [{ "italic": true, "text": "newh" }]
}
],
"id": "10207a886ca241419b66a3b155bac028",
"attrs": { "dirty": true }
},
{
"type": "blockquote",
"children": [
{ "text": "blockquotes\n\n", "italic": true, "underline": true },
{ "text": "blockquotes", "underline": true, "bold": true }
],
"attrs": { "dirty": true }
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "text": "" }]
},
{
"type": "h1",
"attrs": { "dirty": true },
"children": [{ "text": "Heading 1", "bold": true, "underline": true }]
},
{
"type": "h2",
"attrs": { "dirty": true },
"children": [{ "text": "heading 2", "italic": true }]
},
{
"type": "h3",
"attrs": { "dirty": true },
"children": [
{ "text": "heading 3", "italic": true, "underline": true }
]
},
{
"type": "h4",
"attrs": { "dirty": true },
"children": [{ "text": "heading 4", "bold": true }]
},
{
"type": "h5",
"attrs": { "dirty": true },
"children": [{ "text": "heading 5", "underline": true }]
},
{
"type": "h6",
"attrs": { "dirty": true },
"children": [{ "text": "heading 6", "italic": true }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "italic": true, "text": "" }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "italic": true, "text": "" }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [
{ "italic": true, "text": "" },
{
"type": "a",
"attrs": {
"url": "www.google.com",
"target": "_self",
"dirty": true
},
"children": [{ "text": "Google" }]
},
{ "text": "" }
]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "text": "" }]
},
{
"type": "code",
"attrs": { "language": "html", "dirty": true },
"children": [{ "text": "hi code here" }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [
{ "text": "" },
{
"type": "reference",
"attrs": {
"display-type": "inline",
"type": "entry",
"class-name": "embedded-entry redactor-component inline-entry",
"entry-uid": "bltccc6338d24478e6f",
"locale": "en-us",
"content-type-uid": "rte",
"dirty": true
},
"children": [{ "text": "" }]
},
{ "text": "" }
]
}
]
}
]
{
"nodeType": "document",
"data": {},
"content": [
{
"nodeType": "ordered-list",
"content": [
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "order list value 1",
"marks": [],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "order list value 2 with bold and italic",
"marks": [
{
"type": "bold"
},
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-3",
"content": [
{
"nodeType": "text",
"value": "with Heading 3 value",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "hi normal text",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "bold text here",
"marks": [
{
"type": "bold"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "bold and italic same data test",
"marks": [
{
"type": "bold"
},
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
}
]
},
我已经想出了一个 switch 语句来检查传入的数据并将其存储在与我从第一个对象获取的相同格式或位置
Mapper.js
const {
bold,
italic,
underline,
h1,
h2,
} = require("./Schema");
module.exports = jsonRTEMapper = (data) => {
const jsonSchema = [];
for (const nodeType of data) {
if (
nodeType.content.length !== undefined &&
nodeType.content.length !== 0
) {
switch (nodeType.nodeType) {
case "paragraph":
for (const mark of nodeType.content) {
if (
mark.marks !== undefined &&
mark.marks.length !== undefined &&
mark.marks.length !== 0
) {
for (const type of mark.marks) {
switch (type.type) {
case "bold":
jsonSchema.push(bold(nodeType.content));
break;
case "italic":
jsonSchema.push(italic(nodeType.content));
break;
case "underline":
jsonSchema.push(underline(nodeType.content));
break;
case "code":
jsonSchema.push(code(nodeType.content));
break;
}
}
}
}
break;
case "heading-1":
jsonSchema.push(h1(nodeType.content));
break;
case "heading-2":
jsonSchema.push(h2(nodeType.content));
break;
}
}
}
};
并且在 jsonSchema 中,我正在推送 return 从我在不同文件中编写的函数中获得的数据
Schema.js
function bold(data) {
for (const value of data) {
return {
type: "p",
attrs: { dirty: true },
children: [{ text: value.value, bold: true }],
};
}
}
function italic(data) {
for (const value of data) {
return {
type: "p",
attrs: { dirty: true },
children: [{ text: value.value, italic: true }],
};
}
}
function h1(data) {
for (const value of data) {
return {
type: "h1",
attrs: { dirty: true },
children: [{ text: value.value }],
};
}
}
function h2(data) {
for (const value of data) {
return {
type: "h2",
attrs: { dirty: true },
children: [{ text: value.value }],
};
}
}
问题是当类型是单一类型时它工作得很好,例如如果类型是船或内部或下划线它工作正常但是当类型包含不止一种类型时如何使用使预期的输出是我做不到
因为我正在寻找解决方案,如果任何对象包含类型:“粗体”type:italic,它应该替换为 bold:true italic:true 或下划线或 type:code 内
只是想替换此类型并按预期获取 json 对象 json 对象但不知道该怎么做
如果您将所有 mappers/parsers 放在一个映射中,您将不必使用 switch case 语句。而且事情会更容易实施:
// parsers.js ------------------------------
// put all parsing logic in this file
const parsers = new Map();
// once all the parsers are implemented you don't need this check
parsers._get = parsers.get;
parsers.get = function(key) {
if (this.has(key))
return this._get(key);
return () => console.warn(`Parser not implemented for: ${JSON.stringify(key)}`);
}
// map all the parsers
// keys should be exactly same as keys in input json
parsers.set('document', parseDocument);
parsers.set('paragraph', parseParagraph);
parsers.set('text', parseText);
parsers.set('hr', parseHR);
parsers.set('list-item', parseLI);
parsers.set('unordered-list', parseUL);
parsers.set('ordered-list', parseOL);
// TODO: implement remaining 15 element types
//parsers.set('', parse);
function convert(obj) {
return [parsers.get(obj.nodeType)(obj)];
};
function parseDocument(obj) {
let type = 'doc';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseParagraph(obj) {
let type = 'p';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseText(obj) {
const result = {};
result.text = obj.value;
obj.marks.forEach(e => result[e.type] = true);
return result;
}
function parseHR(obj) {
let type = 'hr';
let attrs = { "dirty": true }
let children = [];
return { type, attrs, children };
}
function parseUL(obj) {
let type = 'ul';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseOL(obj) {
let type = 'ol';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseLI(obj) {
let type = 'li';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
// get rid of paragraph elements
children = children.map(c => c.children).flat();
return { type, children, attrs };
}
// mapper.js ---------------------------------------------
let result = convert(getSrcData());
console.log('Converted object: ', result);
let result2 = convert(getSrcData2());
console.log('Converted object2: ', result2);
function getSrcData() {
return {
"nodeType": "document",
"data": {},
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "dummy testing bold",
"marks": [{
"type": "bold"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "jsdbhcjbjsdz'c;s bold and italic",
"marks": [{
"type": "bold"
},
{
"type": "italic"
}
],
"data": {}
}],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "underline bold and italic",
"marks": [{
"type": "bold"
},
{
"type": "italic"
},
{
"type": "underline"
}
],
"data": {}
}],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "ldwc;s,l;,aws italic",
"marks": [{
"type": "italic"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "hr",
"content": [],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "testing",
"marks": [{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}],
"data": {}
},
{
"nodeType": "unordered-list",
"content": [{
"nodeType": "list-item",
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "hello ji",
"marks": [{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "list-item",
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "nahi ji",
"marks": [{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "list-item",
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "testing ji",
"marks": [{
"type": "underline"
},
{
"type": "italic"
},
{
"type": "bold"
}
],
"data": {}
}],
"data": {}
}],
"data": {}
}
],
"data": {}
},
{
"nodeType": "ordered-list",
"content": [{
"nodeType": "list-item",
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "testing",
"marks": [{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "list-item",
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "newh",
"marks": [{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}],
"data": {}
}],
"data": {}
}
],
"data": {}
},
{
"nodeType": "blockquote",
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "blockquotes",
"marks": [{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "blockquotes",
"marks": [{
"type": "bold"
},
{
"type": "code"
},
{
"type": "underline"
}
],
"data": {}
}],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-1",
"content": [{
"nodeType": "text",
"value": "Heading 1",
"marks": [{
"type": "bold"
},
{
"type": "underline"
}
],
"data": {}
}],
"data": {}
},
{
"nodeType": "heading-2",
"content": [{
"nodeType": "text",
"value": "heading 2",
"marks": [{
"type": "italic"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "heading-3",
"content": [{
"nodeType": "text",
"value": "heading 3",
"marks": [{
"type": "italic"
},
{
"type": "underline"
}
],
"data": {}
}],
"data": {}
},
{
"nodeType": "heading-4",
"content": [{
"nodeType": "text",
"value": "heading 4",
"marks": [{
"type": "bold"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "heading-5",
"content": [{
"nodeType": "text",
"value": "heading 5",
"marks": [{
"type": "underline"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "heading-6",
"content": [{
"nodeType": "text",
"value": "heading 6",
"marks": [{
"type": "italic"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "hr",
"content": [],
"data": {}
},
{
"nodeType": "embedded-entry-block",
"content": [],
"data": {
"target": {
"sys": {
"id": "651dYO8d4oE0MVJlVePWr6",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "hyperlink",
"content": [{
"nodeType": "text",
"value": "Google",
"marks": [],
"data": {}
}],
"data": {
"uri": "www.google.com"
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "entry-hyperlink",
"content": [{
"nodeType": "text",
"value": "Entry",
"marks": [],
"data": {}
}],
"data": {
"target": {
"sys": {
"id": "66rzYr2BpWL1VTBHdLTdSW",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "asset-hyperlink",
"content": [{
"nodeType": "text",
"value": "Asset",
"marks": [],
"data": {}
}],
"data": {
"target": {
"sys": {
"id": "2FfbMwKRZ2QK52MsbYxZbb",
"type": "Link",
"linkType": "Asset"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "hi code here",
"marks": [{
"type": "code"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "embedded-entry-block",
"content": [],
"data": {
"target": {
"sys": {
"id": "4zu7f2YP4cXqR6aeeMxEt8",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "embedded-entry-inline",
"content": [],
"data": {
"target": {
"sys": {
"id": "4zu7f2YP4cXqR6aeeMxEt8",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "embedded-entry-block",
"content": [],
"data": {
"target": {
"sys": {
"id": "2eAmIIuG4xkLvatkU3RUSy",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "embedded-entry-inline",
"content": [],
"data": {
"target": {
"sys": {
"id": "49VJ3thpTULacWCcnjbOXz",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
}
]
}
}
function getSrcData2() {
return {
"nodeType": "document",
"data": {},
"content": [
{
"nodeType": "ordered-list",
"content": [
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "order list value 1",
"marks": [],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "order list value 2 with bold and italic",
"marks": [
{
"type": "bold"
},
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-3",
"content": [
{
"nodeType": "text",
"value": "with Heading 3 value",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "hi normal text",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "bold text here",
"marks": [
{
"type": "bold"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "bold and italic same data test",
"marks": [
{
"type": "bold"
},
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
}
]
};
}
<b>See console logs for the output.</b>
解释在代码注释中。我已经为 6 种元素类型制定了框架并实施了解析器。休息你可以轻松实施。在实现其余类型时,您会很好地理解代码。
- 使用 Developer tool 的 code debugging features to understand the code. Or use VSCode to debug nodejs 代码。
- 使用 http://jsonviewer.stack.hu 分析输入和输出 JSON。
此处 data1
和 content
的顺序每次都会更改,这就是我使用 switch 语句的原因
我是运行一个函数,通过这样传递值
parser(values[key]);
这个值[键]) 包含波纹管对象 以下对象/数据结构
{
"nodeType": "document",
"data": {},
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "dummy testing bold",
"marks": [
{
"type": "bold"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "jsdbhcjbjsdz'c;s bold and italic",
"marks": [
{
"type": "bold"
},
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "underline bold and italic",
"marks": [
{
"type": "bold"
},
{
"type": "italic"
},
{
"type": "underline"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "ldwc;s,l;,aws italic",
"marks": [
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "hr",
"content": [],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "testing",
"marks": [
{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "unordered-list",
"content": [
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "hello ji",
"marks": [
{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "nahi ji",
"marks": [
{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "testing ji",
"marks": [
{
"type": "underline"
},
{
"type": "italic"
},
{
"type": "bold"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "ordered-list",
"content": [
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "testing",
"marks": [
{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "newh",
"marks": [
{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "blockquote",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "blockquotes",
"marks": [
{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "blockquotes",
"marks": [
{
"type": "bold"
},
{
"type": "code"
},
{
"type": "underline"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-1",
"content": [
{
"nodeType": "text",
"value": "Heading 1",
"marks": [
{
"type": "bold"
},
{
"type": "underline"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-2",
"content": [
{
"nodeType": "text",
"value": "heading 2",
"marks": [
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-3",
"content": [
{
"nodeType": "text",
"value": "heading 3",
"marks": [
{
"type": "italic"
},
{
"type": "underline"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-4",
"content": [
{
"nodeType": "text",
"value": "heading 4",
"marks": [
{
"type": "bold"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-5",
"content": [
{
"nodeType": "text",
"value": "heading 5",
"marks": [
{
"type": "underline"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-6",
"content": [
{
"nodeType": "text",
"value": "heading 6",
"marks": [
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "hr",
"content": [],
"data": {}
},
{
"nodeType": "embedded-entry-block",
"content": [],
"data": {
"target": {
"sys": {
"id": "651dYO8d4oE0MVJlVePWr6",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "hyperlink",
"content": [
{
"nodeType": "text",
"value": "Google",
"marks": [],
"data": {}
}
],
"data": {
"uri": "www.google.com"
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "entry-hyperlink",
"content": [
{
"nodeType": "text",
"value": "Entry",
"marks": [],
"data": {}
}
],
"data": {
"target": {
"sys": {
"id": "66rzYr2BpWL1VTBHdLTdSW",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "asset-hyperlink",
"content": [
{
"nodeType": "text",
"value": "Asset",
"marks": [],
"data": {}
}
],
"data": {
"target": {
"sys": {
"id": "2FfbMwKRZ2QK52MsbYxZbb",
"type": "Link",
"linkType": "Asset"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "hi code here",
"marks": [
{
"type": "code"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "embedded-entry-block",
"content": [],
"data": {
"target": {
"sys": {
"id": "4zu7f2YP4cXqR6aeeMxEt8",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "embedded-entry-inline",
"content": [],
"data": {
"target": {
"sys": {
"id": "4zu7f2YP4cXqR6aeeMxEt8",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "embedded-entry-block",
"content": [],
"data": {
"target": {
"sys": {
"id": "2eAmIIuG4xkLvatkU3RUSy",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "embedded-entry-inline",
"content": [],
"data": {
"target": {
"sys": {
"id": "49VJ3thpTULacWCcnjbOXz",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
}
]
}
...应转换为另一种自定义结构,如下所示...
[
{
"type": "doc",
"attrs": { "dirty": true },
"children": [
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "text": "dummy testing bold", "bold": true }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [
{
"text": "jsdbhcjbjsdz'c;s bold and italic",
"bold": true,
"italic": true
}
]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [
{
"text": "underline bold and italic",
"bold": true,
"italic": true,
"underline": true
}
]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "text": "idwc:s,l:.aws italic", "italic": true }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "italic": true, "text": "" }]
},
{
"type": "hr",
"children": [{ "text": "" }],
"attrs": { "dirty": true }
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "text": "" }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "text": "testing", "italic": true }]
},
{
"type": "ul",
"children": [
{
"type": "li",
"attrs": { "dirty": true },
"children": [{ "italic": true, "text": "hello ji" }]
},
{
"type": "li",
"attrs": { "dirty": true },
"children": [{ "italic": true, "text": "nahi ji" }]
},
{
"type": "li",
"attrs": { "dirty": true },
"children": [
{
"text": "testing ji",
"italic": true,
"bold": true,
"underline": true
}
]
}
],
"id": "6c180306f6204829999b78e8dd482d33",
"attrs": { "dirty": true }
},
{
"type": "ol",
"children": [
{
"type": "li",
"children": [{ "text": "testing", "italic": true }],
"attrs": { "dirty": true }
},
{
"type": "li",
"attrs": { "dirty": true },
"children": [{ "italic": true, "text": "newh" }]
}
],
"id": "10207a886ca241419b66a3b155bac028",
"attrs": { "dirty": true }
},
{
"type": "blockquote",
"children": [
{ "text": "blockquotes\n\n", "italic": true, "underline": true },
{ "text": "blockquotes", "underline": true, "bold": true }
],
"attrs": { "dirty": true }
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "text": "" }]
},
{
"type": "h1",
"attrs": { "dirty": true },
"children": [{ "text": "Heading 1", "bold": true, "underline": true }]
},
{
"type": "h2",
"attrs": { "dirty": true },
"children": [{ "text": "heading 2", "italic": true }]
},
{
"type": "h3",
"attrs": { "dirty": true },
"children": [
{ "text": "heading 3", "italic": true, "underline": true }
]
},
{
"type": "h4",
"attrs": { "dirty": true },
"children": [{ "text": "heading 4", "bold": true }]
},
{
"type": "h5",
"attrs": { "dirty": true },
"children": [{ "text": "heading 5", "underline": true }]
},
{
"type": "h6",
"attrs": { "dirty": true },
"children": [{ "text": "heading 6", "italic": true }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "italic": true, "text": "" }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "italic": true, "text": "" }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [
{ "italic": true, "text": "" },
{
"type": "a",
"attrs": {
"url": "www.google.com",
"target": "_self",
"dirty": true
},
"children": [{ "text": "Google" }]
},
{ "text": "" }
]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [{ "text": "" }]
},
{
"type": "code",
"attrs": { "language": "html", "dirty": true },
"children": [{ "text": "hi code here" }]
},
{
"type": "p",
"attrs": { "dirty": true },
"children": [
{ "text": "" },
{
"type": "reference",
"attrs": {
"display-type": "inline",
"type": "entry",
"class-name": "embedded-entry redactor-component inline-entry",
"entry-uid": "bltccc6338d24478e6f",
"locale": "en-us",
"content-type-uid": "rte",
"dirty": true
},
"children": [{ "text": "" }]
},
{ "text": "" }
]
}
]
}
]
{
"nodeType": "document",
"data": {},
"content": [
{
"nodeType": "ordered-list",
"content": [
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "order list value 1",
"marks": [],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "order list value 2 with bold and italic",
"marks": [
{
"type": "bold"
},
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-3",
"content": [
{
"nodeType": "text",
"value": "with Heading 3 value",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "hi normal text",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "bold text here",
"marks": [
{
"type": "bold"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "bold and italic same data test",
"marks": [
{
"type": "bold"
},
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
}
]
},
我已经想出了一个 switch 语句来检查传入的数据并将其存储在与我从第一个对象获取的相同格式或位置
Mapper.js
const {
bold,
italic,
underline,
h1,
h2,
} = require("./Schema");
module.exports = jsonRTEMapper = (data) => {
const jsonSchema = [];
for (const nodeType of data) {
if (
nodeType.content.length !== undefined &&
nodeType.content.length !== 0
) {
switch (nodeType.nodeType) {
case "paragraph":
for (const mark of nodeType.content) {
if (
mark.marks !== undefined &&
mark.marks.length !== undefined &&
mark.marks.length !== 0
) {
for (const type of mark.marks) {
switch (type.type) {
case "bold":
jsonSchema.push(bold(nodeType.content));
break;
case "italic":
jsonSchema.push(italic(nodeType.content));
break;
case "underline":
jsonSchema.push(underline(nodeType.content));
break;
case "code":
jsonSchema.push(code(nodeType.content));
break;
}
}
}
}
break;
case "heading-1":
jsonSchema.push(h1(nodeType.content));
break;
case "heading-2":
jsonSchema.push(h2(nodeType.content));
break;
}
}
}
};
并且在 jsonSchema 中,我正在推送 return 从我在不同文件中编写的函数中获得的数据
Schema.js
function bold(data) {
for (const value of data) {
return {
type: "p",
attrs: { dirty: true },
children: [{ text: value.value, bold: true }],
};
}
}
function italic(data) {
for (const value of data) {
return {
type: "p",
attrs: { dirty: true },
children: [{ text: value.value, italic: true }],
};
}
}
function h1(data) {
for (const value of data) {
return {
type: "h1",
attrs: { dirty: true },
children: [{ text: value.value }],
};
}
}
function h2(data) {
for (const value of data) {
return {
type: "h2",
attrs: { dirty: true },
children: [{ text: value.value }],
};
}
}
问题是当类型是单一类型时它工作得很好,例如如果类型是船或内部或下划线它工作正常但是当类型包含不止一种类型时如何使用使预期的输出是我做不到
因为我正在寻找解决方案,如果任何对象包含类型:“粗体”type:italic,它应该替换为 bold:true italic:true 或下划线或 type:code 内
只是想替换此类型并按预期获取 json 对象 json 对象但不知道该怎么做
如果您将所有 mappers/parsers 放在一个映射中,您将不必使用 switch case 语句。而且事情会更容易实施:
// parsers.js ------------------------------
// put all parsing logic in this file
const parsers = new Map();
// once all the parsers are implemented you don't need this check
parsers._get = parsers.get;
parsers.get = function(key) {
if (this.has(key))
return this._get(key);
return () => console.warn(`Parser not implemented for: ${JSON.stringify(key)}`);
}
// map all the parsers
// keys should be exactly same as keys in input json
parsers.set('document', parseDocument);
parsers.set('paragraph', parseParagraph);
parsers.set('text', parseText);
parsers.set('hr', parseHR);
parsers.set('list-item', parseLI);
parsers.set('unordered-list', parseUL);
parsers.set('ordered-list', parseOL);
// TODO: implement remaining 15 element types
//parsers.set('', parse);
function convert(obj) {
return [parsers.get(obj.nodeType)(obj)];
};
function parseDocument(obj) {
let type = 'doc';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseParagraph(obj) {
let type = 'p';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseText(obj) {
const result = {};
result.text = obj.value;
obj.marks.forEach(e => result[e.type] = true);
return result;
}
function parseHR(obj) {
let type = 'hr';
let attrs = { "dirty": true }
let children = [];
return { type, attrs, children };
}
function parseUL(obj) {
let type = 'ul';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseOL(obj) {
let type = 'ol';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
return { type, children, attrs };
}
function parseLI(obj) {
let type = 'li';
let attrs = { "dirty": true }
let children = [];
obj.content.forEach(e => children.push(parsers.get(e.nodeType)(e)));
// get rid of paragraph elements
children = children.map(c => c.children).flat();
return { type, children, attrs };
}
// mapper.js ---------------------------------------------
let result = convert(getSrcData());
console.log('Converted object: ', result);
let result2 = convert(getSrcData2());
console.log('Converted object2: ', result2);
function getSrcData() {
return {
"nodeType": "document",
"data": {},
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "dummy testing bold",
"marks": [{
"type": "bold"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "jsdbhcjbjsdz'c;s bold and italic",
"marks": [{
"type": "bold"
},
{
"type": "italic"
}
],
"data": {}
}],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "underline bold and italic",
"marks": [{
"type": "bold"
},
{
"type": "italic"
},
{
"type": "underline"
}
],
"data": {}
}],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "ldwc;s,l;,aws italic",
"marks": [{
"type": "italic"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "hr",
"content": [],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "testing",
"marks": [{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}],
"data": {}
},
{
"nodeType": "unordered-list",
"content": [{
"nodeType": "list-item",
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "hello ji",
"marks": [{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "list-item",
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "nahi ji",
"marks": [{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "list-item",
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "testing ji",
"marks": [{
"type": "underline"
},
{
"type": "italic"
},
{
"type": "bold"
}
],
"data": {}
}],
"data": {}
}],
"data": {}
}
],
"data": {}
},
{
"nodeType": "ordered-list",
"content": [{
"nodeType": "list-item",
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "testing",
"marks": [{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "list-item",
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "newh",
"marks": [{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}],
"data": {}
}],
"data": {}
}
],
"data": {}
},
{
"nodeType": "blockquote",
"content": [{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "blockquotes",
"marks": [{
"type": "italic"
},
{
"type": "code"
}
],
"data": {}
}],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "blockquotes",
"marks": [{
"type": "bold"
},
{
"type": "code"
},
{
"type": "underline"
}
],
"data": {}
}],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-1",
"content": [{
"nodeType": "text",
"value": "Heading 1",
"marks": [{
"type": "bold"
},
{
"type": "underline"
}
],
"data": {}
}],
"data": {}
},
{
"nodeType": "heading-2",
"content": [{
"nodeType": "text",
"value": "heading 2",
"marks": [{
"type": "italic"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "heading-3",
"content": [{
"nodeType": "text",
"value": "heading 3",
"marks": [{
"type": "italic"
},
{
"type": "underline"
}
],
"data": {}
}],
"data": {}
},
{
"nodeType": "heading-4",
"content": [{
"nodeType": "text",
"value": "heading 4",
"marks": [{
"type": "bold"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "heading-5",
"content": [{
"nodeType": "text",
"value": "heading 5",
"marks": [{
"type": "underline"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "heading-6",
"content": [{
"nodeType": "text",
"value": "heading 6",
"marks": [{
"type": "italic"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "hr",
"content": [],
"data": {}
},
{
"nodeType": "embedded-entry-block",
"content": [],
"data": {
"target": {
"sys": {
"id": "651dYO8d4oE0MVJlVePWr6",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "hyperlink",
"content": [{
"nodeType": "text",
"value": "Google",
"marks": [],
"data": {}
}],
"data": {
"uri": "www.google.com"
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "entry-hyperlink",
"content": [{
"nodeType": "text",
"value": "Entry",
"marks": [],
"data": {}
}],
"data": {
"target": {
"sys": {
"id": "66rzYr2BpWL1VTBHdLTdSW",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "asset-hyperlink",
"content": [{
"nodeType": "text",
"value": "Asset",
"marks": [],
"data": {}
}],
"data": {
"target": {
"sys": {
"id": "2FfbMwKRZ2QK52MsbYxZbb",
"type": "Link",
"linkType": "Asset"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "hi code here",
"marks": [{
"type": "code"
}],
"data": {}
}],
"data": {}
},
{
"nodeType": "embedded-entry-block",
"content": [],
"data": {
"target": {
"sys": {
"id": "4zu7f2YP4cXqR6aeeMxEt8",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}],
"data": {}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "embedded-entry-inline",
"content": [],
"data": {
"target": {
"sys": {
"id": "4zu7f2YP4cXqR6aeeMxEt8",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "embedded-entry-block",
"content": [],
"data": {
"target": {
"sys": {
"id": "2eAmIIuG4xkLvatkU3RUSy",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "paragraph",
"content": [{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
},
{
"nodeType": "embedded-entry-inline",
"content": [],
"data": {
"target": {
"sys": {
"id": "49VJ3thpTULacWCcnjbOXz",
"type": "Link",
"linkType": "Entry"
}
}
}
},
{
"nodeType": "text",
"value": "",
"marks": [],
"data": {}
}
],
"data": {}
}
]
}
}
function getSrcData2() {
return {
"nodeType": "document",
"data": {},
"content": [
{
"nodeType": "ordered-list",
"content": [
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "order list value 1",
"marks": [],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "list-item",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "order list value 2 with bold and italic",
"marks": [
{
"type": "bold"
},
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "heading-3",
"content": [
{
"nodeType": "text",
"value": "with Heading 3 value",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "hi normal text",
"marks": [],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "bold text here",
"marks": [
{
"type": "bold"
}
],
"data": {}
}
],
"data": {}
},
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "bold and italic same data test",
"marks": [
{
"type": "bold"
},
{
"type": "italic"
}
],
"data": {}
}
],
"data": {}
}
]
};
}
<b>See console logs for the output.</b>
解释在代码注释中。我已经为 6 种元素类型制定了框架并实施了解析器。休息你可以轻松实施。在实现其余类型时,您会很好地理解代码。
- 使用 Developer tool 的 code debugging features to understand the code. Or use VSCode to debug nodejs 代码。
- 使用 http://jsonviewer.stack.hu 分析输入和输出 JSON。