解析递归JSON,来自Markdown Ast

发布于 2025-01-22 17:17:37 字数 7659 浏览 0 评论 0原文

递归功能不是我的堡垒,我很确定这里需要什么。

我有一个嵌套的JSON对象,该对象表示嵌套的清单 It is autogenerated from markdown using mdast-util-gfm-task-列表项目

* AAA
    * BBB
* CCC
    * [x] DDD
        * [ ] EEE
        * FFF
    {
    "type": "root",
    "children": [
        {
            "type": "list",
            "ordered": false,
            "start": null,
            "spread": false,
            "children": [
                {
                    "type": "listItem",
                    "spread": false,
                    "checked": null,
                    "children": [
                        {
                            "type": "paragraph",
                            "children": [
                                {
                                    "type": "text",
                                    "value": "AAA"
                                }
                            ]
                        },
                        {
                            "type": "list",
                            "ordered": false,
                            "start": null,
                            "spread": false,
                            "children": [
                                {
                                    "type": "listItem",
                                    "spread": false,
                                    "checked": null,
                                    "children": [
                                        {
                                            "type": "paragraph",
                                            "children": [
                                                {
                                                    "type": "text",
                                                    "value": "BBB"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                },
                {
                    "type": "listItem",
                    "spread": false,
                    "checked": null,
                    "children": [
                        {
                            "type": "paragraph",
                            "children": [
                                {
                                    "type": "text",
                                    "value": "CCC"
                                }
                            ]
                        },
                        {
                            "type": "list",
                            "ordered": false,
                            "start": null,
                            "spread": false,
                            "children": [
                                {
                                    "type": "listItem",
                                    "spread": false,
                                    "checked": null,
                                    "children": [
                                        {
                                            "type": "paragraph",
                                            "children": [
                                                {
                                                    "type": "text",
                                                    "value": "DDD"
                                                }
                                            ]
                                        },
                                        {
                                            "type": "list",
                                            "ordered": false,
                                            "start": null,
                                            "spread": false,
                                            "children": [
                                                {
                                                    "type": "listItem",
                                                    "spread": false,
                                                    "checked": null,
                                                    "children": [
                                                        {
                                                            "type": "paragraph",
                                                            "children": [
                                                                {
                                                                    "type": "text",
                                                                    "value": "EEE"
                                                                }
                                                            ]
                                                        }
                                                    ]
                                                },
                                                {
                                                    "type": "listItem",
                                                    "spread": false,
                                                    "checked": null,
                                                    "children": [
                                                        {
                                                            "type": "paragraph",
                                                            "children": [
                                                                {
                                                                    "type": "text",
                                                                    "value": "FFF"
                                                                }
                                                            ]
                                                        }
                                                    ]
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

我需要操纵它以采用以下内容,更多的共识格式:

{
    "type" : "list",
    "data" : {
        "style" : "unordered",
        "items" : [
            {
              "content": "AAA",
              "checked" : null
              "items": [
                {
                  "content": "BBB",
                  "checked" : null
                  "items": []
                }
              ]
            },
            {
              "content": "CCC",
              "checked" : null
              "items": [
                {
                  "content": "DDD",
                  "checked": true,
                  "items": [
                      {
                        "content": "EEE",
                        "checked": false,
                        "items": []
                      },
                      {
                        "content": "FFF",
                        "checked": null,
                         "items": []
                      }
                   ]
                },
              ]
            }
        ]
    }
}

任何帮助都非常感谢 - 我一直在墙上撞了几个小时。

Recursive functions are not my forté, and im pretty sure thats what is needed here.

I have a nested json object, which represents a nested checklist
It is autogenerated from markdown using mdast-util-gfm-task-list-item

* AAA
    * BBB
* CCC
    * [x] DDD
        * [ ] EEE
        * FFF
    {
    "type": "root",
    "children": [
        {
            "type": "list",
            "ordered": false,
            "start": null,
            "spread": false,
            "children": [
                {
                    "type": "listItem",
                    "spread": false,
                    "checked": null,
                    "children": [
                        {
                            "type": "paragraph",
                            "children": [
                                {
                                    "type": "text",
                                    "value": "AAA"
                                }
                            ]
                        },
                        {
                            "type": "list",
                            "ordered": false,
                            "start": null,
                            "spread": false,
                            "children": [
                                {
                                    "type": "listItem",
                                    "spread": false,
                                    "checked": null,
                                    "children": [
                                        {
                                            "type": "paragraph",
                                            "children": [
                                                {
                                                    "type": "text",
                                                    "value": "BBB"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                },
                {
                    "type": "listItem",
                    "spread": false,
                    "checked": null,
                    "children": [
                        {
                            "type": "paragraph",
                            "children": [
                                {
                                    "type": "text",
                                    "value": "CCC"
                                }
                            ]
                        },
                        {
                            "type": "list",
                            "ordered": false,
                            "start": null,
                            "spread": false,
                            "children": [
                                {
                                    "type": "listItem",
                                    "spread": false,
                                    "checked": null,
                                    "children": [
                                        {
                                            "type": "paragraph",
                                            "children": [
                                                {
                                                    "type": "text",
                                                    "value": "DDD"
                                                }
                                            ]
                                        },
                                        {
                                            "type": "list",
                                            "ordered": false,
                                            "start": null,
                                            "spread": false,
                                            "children": [
                                                {
                                                    "type": "listItem",
                                                    "spread": false,
                                                    "checked": null,
                                                    "children": [
                                                        {
                                                            "type": "paragraph",
                                                            "children": [
                                                                {
                                                                    "type": "text",
                                                                    "value": "EEE"
                                                                }
                                                            ]
                                                        }
                                                    ]
                                                },
                                                {
                                                    "type": "listItem",
                                                    "spread": false,
                                                    "checked": null,
                                                    "children": [
                                                        {
                                                            "type": "paragraph",
                                                            "children": [
                                                                {
                                                                    "type": "text",
                                                                    "value": "FFF"
                                                                }
                                                            ]
                                                        }
                                                    ]
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

I need to manipulate it to be in the following, more consise format:

{
    "type" : "list",
    "data" : {
        "style" : "unordered",
        "items" : [
            {
              "content": "AAA",
              "checked" : null
              "items": [
                {
                  "content": "BBB",
                  "checked" : null
                  "items": []
                }
              ]
            },
            {
              "content": "CCC",
              "checked" : null
              "items": [
                {
                  "content": "DDD",
                  "checked": true,
                  "items": [
                      {
                        "content": "EEE",
                        "checked": false,
                        "items": []
                      },
                      {
                        "content": "FFF",
                        "checked": null,
                         "items": []
                      }
                   ]
                },
              ]
            }
        ]
    }
}

Any help greatly appreciated - I have been banging my head against the wall for a couple of hours now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东京女 2025-01-29 17:17:37

我想我在这里很近,但无法完成:

function transform(data) {
  const {type} = data;
  if (type === "root") {
    return transform(data.children[0]);
  } else if (type === "list") {
    return {
      type,
      data: {
        style: data.ordered ? "ordered" : "unordered",
        items: data.children.map(child => transform(child))
      }
    }
  } else if (type === "listItem") {
    return {
      type,
      checked: data.checked,
      content: data.children[0].children[0].value,
      items: data.children.filter(child => child.type === "list").map(child => transform(child))
    }
  }
  return data;
}

// Pass your data in here as fromData
const toData = transform(fromData);

// toData would then be the new format

I think I am pretty close here but haven't been able to finish it off:

function transform(data) {
  const {type} = data;
  if (type === "root") {
    return transform(data.children[0]);
  } else if (type === "list") {
    return {
      type,
      data: {
        style: data.ordered ? "ordered" : "unordered",
        items: data.children.map(child => transform(child))
      }
    }
  } else if (type === "listItem") {
    return {
      type,
      checked: data.checked,
      content: data.children[0].children[0].value,
      items: data.children.filter(child => child.type === "list").map(child => transform(child))
    }
  }
  return data;
}

// Pass your data in here as fromData
const toData = transform(fromData);

// toData would then be the new format
深海不蓝 2025-01-29 17:17:37

谢谢@jimthedev,

我还在另一个论坛上获得了一些帮助,这两个答案都很有用。
最后,我很成功地产生了以下功能,该功能几乎非常适合我的用例。

let ast_to_ejs = (input) => {
    const { type, children } = input;
    if (type === "root") {
      const data = {};
      data.style = input.ordered ? 'ordered' : 'unordered';
      data.items = children.map(ast_to_ejs);
      return { type: 'list', data };
  
    } else if (type === 'list') {
       return children.map(ast_to_ejs);
  
    } else if (type === 'listItem') {
      const checked = input.checked;
      const content = children[0].children[0].value;
       const items = children[1] ? ast_to_ejs(children[1]) : [];
       return { content, checked, items };
     }
};

Thanks @jimTheDev

I also recived some assistance on another forum, and both answers were useful.
In the end, I have sucssfully produced the following function, which works almost perfectly for my use case.

let ast_to_ejs = (input) => {
    const { type, children } = input;
    if (type === "root") {
      const data = {};
      data.style = input.ordered ? 'ordered' : 'unordered';
      data.items = children.map(ast_to_ejs);
      return { type: 'list', data };
  
    } else if (type === 'list') {
       return children.map(ast_to_ejs);
  
    } else if (type === 'listItem') {
      const checked = input.checked;
      const content = children[0].children[0].value;
       const items = children[1] ? ast_to_ejs(children[1]) : [];
       return { content, checked, items };
     }
};
つ低調成傷 2025-01-29 17:17:37

我一直使用的版本类似于您自己想出的版本,但是它以不同的编码方式完成,并且从/root/data/data/toce/code>中删除了一层数组,该数组 t在您要求的输出中,没有太多意义。

const convert = ({type, children, ordered, checked}) => 
  type == 'root'
    ? {
        type: 'list', 
        data: {style: ordered ? 'ordered' : 'unordered', items: convert (children [0])}
      }
  : type == 'list'
    ? children .map (convert)
  : type == 'listItem'
    ? {
        content: children [0] .children [0] .value, 
        checked: checked, 
        items: children [1] ? convert (children [1]) : []
      }
  : {error: `unknown type: '${type}'`}

const ast = {type: "root", children: [{type: "list", ordered: false, start: null, spread: false, children: [{type: "listItem", spread: false, checked: null, children: [{type: "paragraph", children: [{type: "text", value: "AAA"}]}, {type: "list", ordered: false, start: null, spread: false, children: [{type: "listItem", spread: false, checked: null, children: [{type: "paragraph", children: [{type: "text", value: "BBB"}]}]}]}]}, {type: "listItem", spread: false, checked: null, children: [{type: "paragraph", children: [{type: "text", value: "CCC"}]}, {type: "list", ordered: false, start: null, spread: false, children: [{type: "listItem", spread: false, checked: true, children: [{type: "paragraph", children: [{type: "text", value: "DDD"}]}, {type: "list", ordered: false, start: null, spread: false, children: [{type: "listItem", spread: false, checked: false, children: [{type: "paragraph", children: [{type: "text", value: "EEE"}]}]}, {type: "listItem", spread: false, checked: null, children: [{type: "paragraph", children: [{type: "text", value: "FFF"}]}]}]}]}]}]}]}]}

console .log (convert (ast))
.as-console-wrapper {max-height: 100% !important; top: 0}

我还将输入更改为表示检查:true for ddd检查:eee> eee以匹配您的示例标记。这两个初始AST具有null。我假设null是针对“此处无复选框”,并且truefalse如果检查或未检查,则将使用。如果不是这种情况,那可能需要一些摆弄。

The version I had been working on is similar to what you figured out yourself, but it's done in a different coding style, and it removes one layer of array from /root/data/items, which wasn't there in your requested output and doesn't make much sense.

const convert = ({type, children, ordered, checked}) => 
  type == 'root'
    ? {
        type: 'list', 
        data: {style: ordered ? 'ordered' : 'unordered', items: convert (children [0])}
      }
  : type == 'list'
    ? children .map (convert)
  : type == 'listItem'
    ? {
        content: children [0] .children [0] .value, 
        checked: checked, 
        items: children [1] ? convert (children [1]) : []
      }
  : {error: `unknown type: '${type}'`}

const ast = {type: "root", children: [{type: "list", ordered: false, start: null, spread: false, children: [{type: "listItem", spread: false, checked: null, children: [{type: "paragraph", children: [{type: "text", value: "AAA"}]}, {type: "list", ordered: false, start: null, spread: false, children: [{type: "listItem", spread: false, checked: null, children: [{type: "paragraph", children: [{type: "text", value: "BBB"}]}]}]}]}, {type: "listItem", spread: false, checked: null, children: [{type: "paragraph", children: [{type: "text", value: "CCC"}]}, {type: "list", ordered: false, start: null, spread: false, children: [{type: "listItem", spread: false, checked: true, children: [{type: "paragraph", children: [{type: "text", value: "DDD"}]}, {type: "list", ordered: false, start: null, spread: false, children: [{type: "listItem", spread: false, checked: false, children: [{type: "paragraph", children: [{type: "text", value: "EEE"}]}]}, {type: "listItem", spread: false, checked: null, children: [{type: "paragraph", children: [{type: "text", value: "FFF"}]}]}]}]}]}]}]}]}

console .log (convert (ast))
.as-console-wrapper {max-height: 100% !important; top: 0}

I also changed the input to represent checked: true for DDD and checked: false for EEE to match your sample markup. The initial AST had null for both of those. I'm assuming that null is meant for "no checkbox here" and true and false would be used if there is one checked or unchecked. If that's not the case, then it might take a bit of fiddling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文