johansatge / obsidian-automatic-table-of-contents

💠 An Obsidian plugin to create a table of contents in a note, that updates itself when the note changes
MIT License
132 stars 12 forks source link

Possibility to set starting point of ToC #37

Open jnyblom opened 7 months ago

jnyblom commented 7 months ago

Hi,

I'd like to be able to set a "starting point" for the table of contents. Say I do not want the first h1 and sub headings to be part of the toc. Ex:

# document information

## table of contents

``table-of-contents
first-heading: 2
``
## changelog

## other stuff

# first section in toc

## etc

# second section in toc

## etc

Would result in table of contents starting from "first section in toc":

Hope this makes sense. Thanks.

likemuuxi commented 4 months ago

可以参考下

function onInsertToc(editor, view) {
  const content = editor.getValue();
  const tocAlreadyExists = content.includes('# 目录');
  if (!tocAlreadyExists) {
    view.sourceMode.cmEditor.exec("goStart");
    const markdown = '# 目录\n```' + codeblockId + '\n```\n';
    editor.replaceRange(markdown, editor.getCursor());
  } else {
    new obsidian.Notice('目录已被添加');
  }
}

function onInsertTocWithDocs(editor, view) {
  const content = editor.getValue();
  const tocAlreadyExists = content.includes('# 目录');
  if (!tocAlreadyExists) {
    view.sourceMode.cmEditor.exec("goStart");
    let markdown = ['# 目录\n```' + codeblockId]
    Object.keys(availableOptions).forEach((optionName) => {
      const option = availableOptions[optionName]
      const comment = option.comment.length > 0 ? ` # ${option.comment}` : ''
      markdown.push(`${optionName}: ${option.default}${comment}`)
    })
    markdown.push('```\n')
    editor.replaceRange(markdown.join('\n'), editor.getCursor())
  } else {
    new obsidian.Notice('目录已被添加');
  }
}