longform
longform copied to clipboard
Compile Step Request: Renumber Footnotes
(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!).
This would be great, thanks! Ulysses can renumber footnotes and it's a Markdown application--now sure how it can accomplish this.
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)
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;
}
};
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 thank you so much, it works like a charm <3
@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!).