Skip to content

Add the ability to increase heading level of transcluded markdown files #8

Description

@cncastillo

Hi,

I just wanted to say that this is a very useful plugin, and it was the only way I managed to include other markdown files inside another for my PhD thesis!

Nevertheless, I think it is missing a feature. Sometimes, it is useful to increment the heading level of the included files. This is a feature that other similar packages have, like pandoc-include. I managed to successfully code this feature by modifying the plugin's Javascript files, but then I realized that the plugin was actually written using Typescript. Sadly, I do not have the time right now to do the translation and create a pull request 😞, but I think what I did could potentially be useful anyway.

For this, I included a function incrementHeadings, with an integer levelIncrement argument:

// New function to increase headings
function incrementHeadings(text, levelIncrement) {
  if (!text || levelIncrement <= 0)
    return text;

  const headingRegex = /^(#+)(\s)/gm;
  return text.replace(headingRegex, (match, hashes, space) => {
    return '#'.repeat(hashes.length + levelIncrement) + space;
  });
}
// I include the increment as a final processing step in the bake function
function sanitizeBakedContent(text, levelIncrement) {
  return incrementHeadings(stripBlockId(stripFrontmatter(text)), levelIncrement);
}

and inside async function bake(app, file, subpath, ancestors, settings) { I included this new setting:

    const baked = sanitizeBakedContent(
      await bake(app, linkedFile, subpath2, newAncestors, settings),
      settings.incrementHeadingLevels // <-- New setting
    );

Finally, for the UI I included

                // Add a new setting for heading incrementing
		new import_obsidian.Setting(contentEl).setName("Increment heading levels").setDesc(
			"Increase the heading levels of transcluded markdown files."
		).addToggle(
			(toggle) => toggle.setValue(settings.incrementHeadingLevels).onChange((value) => {
			  settings.incrementHeadingLevels = value;
			  plugin.saveSettings();
			})
		);

in the function

var BakeModal = class extends import_obsidian.Modal {
  constructor(plugin, file) {

producing:

image

I assume that it is straightforward to do the translation, so hopefully, it helps 😄. I have not tested this with files that could have recursive inclusions. For example, main.md includes chapter.md which includes paragraph.md, as that was not present in my use-case, so maybe the call to incrementHeadings needs to be done in a different part of the code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions