mind-elixir-core icon indicating copy to clipboard operation
mind-elixir-core copied to clipboard

可以把MD文本格式数据传递进行渲染吗?

Open MaxwellEdisons opened this issue 1 year ago • 4 comments

可以把MD文本格式数据传递进行渲染吗?getDataMd这个方法传递进去无效

MaxwellEdisons avatar Jul 17 '23 11:07 MaxwellEdisons

暂时不行,getDataMd 是输出 markdown

SSShooter avatar Jul 17 '23 13:07 SSShooter

@MaxwellEdisons Try this function for converting Markdown to the required node format:

export function markdownToJSON(markdown: string): Node {
  const lines = markdown.split("\n")
  const root: Node = {
    nodeData: {
      id: "root",
      topic: lines[0].substring(2),
      root: true,
      expanded: true,
      children: [],
    },
    linkData: {},
  }

  let lastNodes: NodeData[] = [root.nodeData] // The last node of each depth level, index of array is level
  let lastDirection = 0

  for (let i = 1; i < lines.length; i++) {
    if (lines[i].startsWith("#")) {
      // Only parse headers
      let level = lines[i].split(" ")[0].length // use split and length to determine the level
      let node: NodeData = {
        topic: lines[i].substring(level + 1).trim(),
        id: generateId(),
        direction: level === 2 ? lastDirection : 0,
        expanded: true,
        children: [],
      }

      if (level === 2) {
        if (lastDirection === 1) {
          lastDirection = 0
        } else {
          lastDirection = 1
        }
      }

      if (lastNodes.length < level) {
        lastNodes.push(node)
      } else {
        lastNodes[level - 1] = node
        lastNodes = lastNodes.slice(0, level)
      }

      // Add node as child to previous level node
      lastNodes[level - 2].children.push(node)
    } else if (lines[i].trim().length !== 0) {
      // If it's a non-empty non-header line
      // Append line to topic of last node
      lastNodes[lastNodes.length - 1].topic += "\n" + lines[i].trim()
    }
  }

  return root
}

type LinkData = {}

type NodeData = {
  id: string
  topic: string
  root?: boolean
  direction?: number
  expanded: boolean
  children: NodeData[]
}

type Node = {
  nodeData: NodeData
  linkData: LinkData
}

function generateId(): string {
  return (
    Math.random().toString(36).substring(2, 15) +
    Math.random().toString(36).substring(2, 15)
  )
}

There may be some bugs in there, but it should work for most cases

fkloepper avatar Jul 17 '23 20:07 fkloepper

thanks you are awesome

MaxwellEdisons avatar Jul 18 '23 14:07 MaxwellEdisons

function markdownToJSON(markdown: string): Node {
  const lines = markdown.split("\n")
  const root: Node = {
    nodeData: {
      id: "root",
      topic: lines[0].substring(2),
      root: true,
      expanded: true,
      children: [],
    },
    linkData: {},
  }

  let lastNodes: NodeData[] = [root.nodeData]
  let listDepth = 0

  for (let i = 1; i < lines.length; i++) {
    const line = lines[i]
    const trimmedLine = line.trim()
    const leadingSpaces = line.length - line.trimStart().length

    if (trimmedLine.startsWith("#")) {
      listDepth = 0  // Reset list depth when encountering a header
      let level = trimmedLine.split(" ")[0].length 
      let node: NodeData = {
        topic: trimmedLine.substring(level + 1).trim(),
        id: generateId(),
        direction: level === 2 ? listDepth % 2 : 0,
        expanded: true,
        children: [],
      }

      while (lastNodes.length >= level) {
        lastNodes.pop();
      }

      lastNodes.push(node);
      lastNodes[lastNodes.length - 2].children.push(node);
    } else if (trimmedLine.startsWith("-")) {
      const listItemLevel = Math.floor(leadingSpaces / 2) + 1  // 2 spaces per level
      listDepth = listItemLevel

      let node: NodeData = {
        topic: trimmedLine.substring(1).trim(),
        id: generateId(),
        listItem: true,
        expanded: true,
        children: [],
      }

      while (lastNodes.length >= listItemLevel + 1) {
        lastNodes.pop();
      }

      lastNodes.push(node);
      lastNodes[lastNodes.length - 2].children.push(node);
    } else if (trimmedLine.length !== 0) {
      lastNodes[lastNodes.length - 1].topic += "\n" + trimmedLine
    }
  }

  return root
}

I made a simple modification to this function, and now I can parse such a format, I hope it will help others - Additional resources: - "Nachrichtenleicht" for news - "My German Short Stories" for listening practice - "Easy German" for street interviews

MaxwellEdisons avatar Jul 25 '23 03:07 MaxwellEdisons