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

option to generate random hash as footnote name #15

Open
juliaanvaneijndt opened this issue Apr 1, 2024 · 0 comments
Open

option to generate random hash as footnote name #15

juliaanvaneijndt opened this issue Apr 1, 2024 · 0 comments

Comments

@juliaanvaneijndt
Copy link

This extension is brilliant!
I've edited locally insertFootnote.js with the following code to mimic the behaviour of this atom extension, which generates a random hash for each footnote and "removes the hassle of having to track footnote numbering through the file or between collaborators".
It works well. Do you think this could be a worthwhile option to add to your extension?

function nextLine(position) {
    return new vscode.Position(position.line + 1, 0);
}

// Function to generate a random hash of 6 letters (uppercase) and digits
function generateRandomHash(length = 6) {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    let result = '';
    for (let i = 0; i < length; i++) {
        result += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return result;
}

function insertFootnote({ footnoteName } = {}) {
    return __awaiter(this, void 0, void 0, function* () {
        const editor = vscode.window.activeTextEditor;
        if (!editor) {
            return;
        }
        let text;
        const shouldInsertFootnoteRef = !footnoteName;

        if (shouldInsertFootnoteRef) {
            const refMatches = utils_1.matchAll(utils_1.footnoteRefRegex, editor.document.getText());
            // Using a random hash as the default footnote name if not provided
            const defaultFootnoteName = generateRandomHash();
            footnoteName =
                (yield vscode.window.showInputBox({
                    prompt: 'Footnote name (no space or tab)',
                    placeHolder: 'Optional: Specific footnote name',
                    value: defaultFootnoteName, // Setting the default value to our random hash
                })) || '';
        }

        footnoteName = footnoteName.replace(/\s/g, '');

        editor.edit((edit) => {
            if (shouldInsertFootnoteRef) {
                edit.insert(editor.selection.start, `[^${footnoteName}]`);
            }

            text = editor.document.getText();
            const emptyLinesAbove = text.slice(-1) === '\n' ? '\n' : '\n\n';
            const endPosition = editor.document.positionAt(text.length);
            edit.insert(endPosition, `${emptyLinesAbove}[^${footnoteName}]: `);
            const newEndPosition = editor.document.positionAt(editor.document.getText().length);

            editor.selection = new vscode.Selection(newEndPosition, newEndPosition); // set cursor
            editor.revealRange(new vscode.Range(newEndPosition, nextLine(newEndPosition))); // scroll to
        });
    });
}

exports.default = insertFootnote;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant