Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix any #155

Open
github-actions bot opened this issue Dec 29, 2023 · 0 comments
Open

fix any #155

github-actions bot opened this issue Dec 29, 2023 · 0 comments
Labels

Comments

@github-actions
Copy link

fix any

        }
      }});
  };
}

// todo maybe markdown file?
interface HeadingNode extends Node {
  depth: number;
  children: { value: string }[];
}

// todo maybe markdown file?
function _remarkGenerateNestedToc() {
  console.log('------------------')
  return (tree: Node, file: VFile) => {
    const headings: { value: string, depth: number, slug: string }[] = []
    visit(tree, 'heading', (node: HeadingNode) => {
      const value = node.children.reduce((text, child) => text + child.value, '')
      const slug = slugger(value)
      headings.push({ value, depth: node.depth, slug })
    });
    file.data.toc = _getNestedToc(headings)
  };
};

function _getNestedToc(markdownHeading: any): TocNode[] {
  let latestEntry: TocNode | null
  let latestParent: TocNode | null
  const markdownHeadingCopy = JSON.parse(JSON.stringify(markdownHeading))
  if (markdownHeadingCopy.length <= 1) return markdownHeadingCopy
  // TODO fix any
  const entryDepth: number[] = markdownHeading.reduce((acc: number, item: any) => {
    return item.depth < acc ? item.depth : acc
  }, Number.POSITIVE_INFINITY)
  // TODO fix any
  return markdownHeadingCopy.reduce((result: any, entry: any) => {
    if (latestEntry && !latestEntry.children) {
      latestEntry.children = []
    }
    const latestEntryDepth = latestEntry?.depth || 0
    const latestEntryChildren = latestEntry?.children || []
    const latestParentChildren = latestParent?.children || []
    if (entry.depth === entryDepth) {
      entry.children = []
      result.push(entry)
      latestParent = null
    } else if (entry.depth === latestEntryDepth + 1) {
      latestEntryChildren.push(entry)
      latestParent = latestEntry
    } else if (entry.depth === latestEntryDepth) {
      latestParentChildren.push(entry)
    } else {
      console.error('Unexpected Toc behaviour', entry)
    }
    latestEntry = entry
    return result
  }, [])
}

50a1ec1160a6babcb65cae046260b8418ea4467e

@github-actions github-actions bot added the todo label Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants