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

Compile Step Request: Renumber Footnotes #44

Closed
kevboh opened this issue Jan 20, 2022 · 6 comments
Closed

Compile Step Request: Renumber Footnotes #44

kevboh opened this issue Jan 20, 2022 · 6 comments
Labels
compile Pertaining to the Compile feature. enhancement New feature or request

Comments

@kevboh
Copy link
Owner

kevboh commented Jan 20, 2022

(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 kevboh added enhancement New feature or request compile Pertaining to the Compile feature. labels Jan 20, 2022
@erazlogo
Copy link

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

@chrisgrieser
Copy link
Contributor

chrisgrieser commented Jan 23, 2022

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
Copy link
Contributor

chrisgrieser commented Jan 28, 2022

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;
	}

};

@ennui2342
Copy link

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;
  }

};

@nicosomb
Copy link

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

@kevboh
Copy link
Owner Author

kevboh commented Jan 3, 2024

@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 kevboh closed this as completed Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compile Pertaining to the Compile feature. enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants