huntertran / markdown-toc

Auto MarkdownTOC (Table Of Contents) plugin for Visual Studio Code.
https://marketplace.visualstudio.com/items?itemName=huntertran.auto-markdown-toc
MIT License
39 stars 25 forks source link

Updating bitbucket.org style TOC inserts empty lines #30

Closed pgroke-dt closed 4 years ago

pgroke-dt commented 4 years ago

Updating a TOC with style bitbucket.org and Insert Anchor enabled inserts one additional empty line per update.

It seems like there's code that tries to prevent this, but has some bugs:

I managed to fix the code directly in the installed AutoMarkdownToc.js, but since that's practically identical to the typescript source, I think sharing it here may help:

    deleteAnchors(editBuilder) {
        let editor = vscode_1.window.activeTextEditor;
        if (editor != undefined) {
            let doc = editor.document;
            for (let index = 0; index < doc.lineCount; index++) {
                let lineText = doc.lineAt(index).text;
                if (lineText.match(RegexStrings_1.RegexStrings.Instance.REGEXP_MARKDOWN_ANCHOR) == null) {
                    continue;
                }
                let startPosition = new vscode_1.Position(index, 0);
                let endPosition = new vscode_1.Position(index + 1, 0);
                // To ensure the anchor will not insert an extra empty line
                if (this.configManager.options.ANCHOR_MODE.value == AnchorMode_1.AnchorMode.bitbucket) {
                    if ((index + 1) < doc.lineCount && doc.lineAt(index + 1).text == "") {
                        endPosition = new vscode_1.Position(index + 2, 0);
                    }
                }
                let range = new vscode_1.Range(startPosition, endPosition);
                editBuilder.delete(range);
            }
        }
    }
huntertran commented 4 years ago

I think this is fixed in 3.0.7

pgroke-dt commented 4 years ago

I just used markdown-toc again, and no, it's not fixed. Every update inserts an empty line + new anchor now.