longform icon indicating copy to clipboard operation
longform copied to clipboard

Compile Step Request: Renumber Footnotes

Open kevboh opened this issue 3 years ago • 3 comments

(inspired by #37)

A manuscript step that finds all the footnotes in a document, renumbers them, and moves them around. I don't personally use markdown footnotes, so some more research is required here before I implement this (if I implement it!).

kevboh avatar Jan 20 '22 03:01 kevboh

This would be great, thanks! Ulysses can renumber footnotes and it's a Markdown application--now sure how it can accomplish this.

erazlogo avatar Jan 21 '22 10:01 erazlogo

I already have everything ready and working bug free as templater script. I'll gladly share it here once I turn it into a compile script step. (probably later this week)

chrisgrieser avatar Jan 23 '22 22:01 chrisgrieser

Here you go. Feel free to sherlock it; some place to "collect" general purpose user scripts might be useful, too?

module.exports = {
	description: {
		name: "Re-Index Footnotes",
		description: "Re-Index Footnote Numbering",
		availableKinds: ["Manuscript"],
		options: []
	},

  compile (input, context) {
		let text = input.contents;

		// re-index footnote-definitions
		let ft_index = 0;
		text = text.replace(/^\[\^\S+?]: /gm, function() {
			ft_index++;
			return ('[^' + String(ft_index) + ']: ');
		});

		// re-index footnote-keys
		// regex uses hack to treat lookahead as lookaround https://stackoverflow.com/a/43232659
		ft_index = 0;
		text = text.replace(/(?!^)\[\^\S+?]/gm, function() {
			ft_index++;
			return ('[^' + String(ft_index) + ']');
		});

		input.contents = text;
		return input;
	}

};

chrisgrieser avatar Jan 28 '22 22:01 chrisgrieser

Given that I sometimes reference a footnote more than once in a scene (including two quotes from a reference for example), the above doesn't work for me. If other's have this problem, here's a variation on Chris' script which prepends the scene number and a hyphen to all footnotes to make them unique in the manuscript:

module.exports = {
        description: {
                name: "Prepend scene number to footnotes",
                description: "Prepend scene number to footnotes",
                availableKinds: ["Scene"],
                options: []
        },

  compile: async (input, context) => {
    for (let i = 0; i < input.length; i++) {
                let text = input[i].contents;
                
                // re-index footnote-definitions
                text = text.replace(/^\[\^(\S+)?]: /gm, function(match, label) {
                        return ('[^' + i + '-' + label + ']: ');
                });
                
                // re-index footnote-keys
                // regex uses hack to treat lookahead as lookaround https://stackoverflow.com/a/43232659
                text = text.replace(/(?!^)\[\^(\S+)?]/gm, function(match, label) {
                        return ('[^' + i + '-' + label + ']');
                });
                
                input[i].contents = text;
        }
        return input;
  }

};

ennui2342 avatar Apr 04 '23 15:04 ennui2342

@ennui2342 thank you so much, it works like a charm <3

nicosomb avatar Nov 21 '23 12:11 nicosomb

@chrisgrieser's version is now at https://github.com/obsidian-community/longform-compile-steps, so I'm going to go ahead and close this issue—trying to thread the needle and only add steps to the core repo when they hit some form of critical mass (mostly because I just don't have the time!).

kevboh avatar Jan 03 '24 02:01 kevboh