feat: add setting to include or exclude filenames form the merged document

This commit is contained in:
AntoKeinanen 2025-06-30 22:33:02 +03:00
parent 18613da93e
commit a7ab037ef5
No known key found for this signature in database
8 changed files with 83 additions and 2538 deletions

View file

@ -2,6 +2,12 @@
### Additions and Changes ### Additions and Changes
- Added a setting to include or exclude filenames form the merged document
## obsidian-advanced-merger 1.6.0
### Additions and Changes
- Added functionality to automatically delete YAML properties - Added functionality to automatically delete YAML properties
## obsidian-advanced-merger 1.5.0 ## obsidian-advanced-merger 1.5.0

View file

@ -1,7 +1,7 @@
{ {
"id": "advanced-merger", "id": "advanced-merger",
"name": "Advanced Merger", "name": "Advanced Merger",
"version": "1.6.0", "version": "1.7.0",
"minAppVersion": "0.15.0", "minAppVersion": "0.15.0",
"description": "Merge a folder of notes for easier export.", "description": "Merge a folder of notes for easier export.",
"author": "Anto Keinänen", "author": "Anto Keinänen",

View file

@ -1,7 +1,7 @@
{ {
"id": "advanced-merger", "id": "advanced-merger",
"name": "Advanced Merger", "name": "Advanced Merger",
"version": "1.6.0", "version": "1.7.0",
"minAppVersion": "0.15.0", "minAppVersion": "0.15.0",
"description": "Merge a folder of notes for easier export.", "description": "Merge a folder of notes for easier export.",
"author": "Anto Keinänen", "author": "Anto Keinänen",

2507
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{ {
"name": "obsidian-advanced-merger", "name": "obsidian-advanced-merger",
"version": "1.6.0", "version": "1.7.0",
"author": { "author": {
"name": "Anto Keinänen", "name": "Anto Keinänen",
"url": "https://github.com/antoKeinanen/" "url": "https://github.com/antoKeinanen/"
@ -13,7 +13,7 @@
"builtin-modules": "3.3.0", "builtin-modules": "3.3.0",
"esbuild": "0.17.3", "esbuild": "0.17.3",
"husky": "^8.0.0", "husky": "^8.0.0",
"obsidian": "latest", "obsidian": "^1.8.7",
"prettier": "^3.0.3", "prettier": "^3.0.3",
"pretty-quick": "^3.1.3", "pretty-quick": "^3.1.3",
"tslib": "2.4.0", "tslib": "2.4.0",
@ -41,5 +41,6 @@
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", "build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
"version": "node version-bump.mjs && git add manifest.json versions.json", "version": "node version-bump.mjs && git add manifest.json versions.json",
"prepare": "husky install" "prepare": "husky install"
} },
"packageManager": "pnpm@10.11.0+sha512.6540583f41cc5f628eb3d9773ecee802f4f9ef9923cc45b69890fb47991d4b092964694ec3a4f738a420c918a333062c8b925d312f42e4f0c263eb603551f977"
} }

View file

@ -110,10 +110,10 @@ export default class AdvancedMerge extends Plugin {
} }
/** /**
* Merges input notes to single output file. * Merges input notes to a single output file.
* @param {Vault} vault - Current vault. * @param vault - Current vault.
* @param {Array<TAbstractFile>} entries - Files and folders, to be included. * @param entries - Files and folders to be included.
* @param {string} outputFileName - Output file name. * @param outputFileName - Output file name.
*/ */
private async mergeNotes( private async mergeNotes(
vault: Vault, vault: Vault,
@ -123,41 +123,50 @@ export default class AdvancedMerge extends Plugin {
const outputFile = await vault.create(outputFileName, ""); const outputFile = await vault.create(outputFileName, "");
for (let index = 0; index < entries.length; index++) { for (let index = 0; index < entries.length; index++) {
const folderOrFile: TAbstractFile = entries[index]; const entry = entries[index];
const sectionLevel = (folderOrFile.path.match(/\//g) || []).length; const sectionLevel = (entry.path.match(/\//g) || []).length;
const isLastEntry = index === entries.length - 1;
let sectionContents = index === 0 ? "" : NEW_LINE_CHAR;
let sectionContents = `${index === 0 ? "" : NEW_LINE_CHAR}`; if (entry instanceof TFile) {
const lastEntry = index === entries.length - 1; const fileContent = await vault.cachedRead(entry);
const fileSectionName = entry.name.replace(/\.md$/, "");
if (this.settings.includeFilenames) {
sectionContents += `${SECTION_CHAR.repeat(sectionLevel)} ${fileSectionName}`;
sectionContents += DOUBLE_NEW_LINE_CHAR;
}
sectionContents += fileContent;
if (!isLastEntry) {
sectionContents += DOUBLE_NEW_LINE_CHAR;
}
if (folderOrFile instanceof TFile) {
sectionContents += await vault.cachedRead(folderOrFile);
const fileSectionName = folderOrFile.name.replace(/\.md$/, "");
// For the first file in a row, we shouldnt add new line
sectionContents = `${SECTION_CHAR.repeat(
sectionLevel,
)} ${fileSectionName}${DOUBLE_NEW_LINE_CHAR}${sectionContents}${
lastEntry ? "" : DOUBLE_NEW_LINE_CHAR
}`;
console.info( console.info(
`Adding file "${folderOrFile.name}" as section "${fileSectionName}" into file "${outputFileName}"..`, `Adding file "${entry.name}" as section "${fileSectionName}" into file "${outputFileName}"..`,
); );
} else if (folderOrFile instanceof TFolder) { } else if (entry instanceof TFolder) {
sectionContents += `${SECTION_CHAR.repeat(sectionLevel)} ${ if (this.settings.includeFilenames) {
folderOrFile.name sectionContents += SECTION_CHAR.repeat(sectionLevel);
}${DOUBLE_NEW_LINE_CHAR}`; sectionContents += entry.name;
}
sectionContents += DOUBLE_NEW_LINE_CHAR;
console.info( console.info(
`Adding folder "${folderOrFile.name}" as section "${sectionContents}" into file "${outputFileName}"..`, `Adding folder "${entry.name}" as section "${entry.name}" into file "${outputFileName}"..`,
); );
} }
if (this.settings.removeYamlProperties) { if (this.settings.removeYamlProperties) {
sectionContents = sectionContents.replace( sectionContents = sectionContents.replace(
/---\n(\w*:\s.*\n)*---/, /^---\n[\s\S]*?\n---\n?/m,
"", "",
); );
} }
vault.append(outputFile, sectionContents); await vault.append(outputFile, sectionContents);
} }
} }
@ -309,6 +318,26 @@ class AdvancedMergeSettingTab extends PluginSettingTab {
}), }),
); );
// Add "remove yaml properties" toggle in settings
new Setting(containerEl)
.setName(this.plugin.translation.get().SettingIncludeFilenames)
.setDesc(
this.plugin.translation.get()
.SettingIncludeFilenamesDescription,
)
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.includeFilenames)
.onChange(async (value) => {
this.plugin.settings.includeFilenames = value;
this.plugin.settings.includeFilenames =
value === false
? value
: this.plugin.settings.includeFilenames;
await this.plugin.saveSettings();
}),
);
this.showIncludeFolderAsSectionSetting(containerEl); this.showIncludeFolderAsSectionSetting(containerEl);
} }

View file

@ -7,6 +7,7 @@ export interface AdvancedMergePluginSettings {
includeNestedFolders: boolean; includeNestedFolders: boolean;
includeFoldersAsSections: boolean; includeFoldersAsSections: boolean;
removeYamlProperties: boolean; removeYamlProperties: boolean;
includeFilenames: boolean;
} }
export const DEFAULT_SETTINGS: AdvancedMergePluginSettings = { export const DEFAULT_SETTINGS: AdvancedMergePluginSettings = {
@ -14,4 +15,5 @@ export const DEFAULT_SETTINGS: AdvancedMergePluginSettings = {
includeNestedFolders: false, includeNestedFolders: false,
includeFoldersAsSections: false, includeFoldersAsSections: false,
removeYamlProperties: false, removeYamlProperties: false,
includeFilenames: true,
}; };

View file

@ -16,6 +16,8 @@ export interface Translation {
SettingIncludeFoldersAsSectionsDescription: string; SettingIncludeFoldersAsSectionsDescription: string;
SettingRemoveYamlProperties: string; SettingRemoveYamlProperties: string;
SettingRemoveYamlPropertiesDescription: string; SettingRemoveYamlPropertiesDescription: string;
SettingIncludeFilenames: string;
SettingIncludeFilenamesDescription: string;
Yes: string; Yes: string;
No: string; No: string;
} }
@ -43,6 +45,8 @@ export const TRANSLATIONS: Record<string, Translation> = {
SettingRemoveYamlProperties: "YAML-Eigenschaften entfernen", SettingRemoveYamlProperties: "YAML-Eigenschaften entfernen",
SettingRemoveYamlPropertiesDescription: SettingRemoveYamlPropertiesDescription:
"Entfernt YAML-Eigenschaften vom Anfang der Datei während des Zusammenführens. WARNUNG: Dies könnte unbeabsichtigt nicht-YAML-Teile entfernen.", "Entfernt YAML-Eigenschaften vom Anfang der Datei während des Zusammenführens. WARNUNG: Dies könnte unbeabsichtigt nicht-YAML-Teile entfernen.",
SettingIncludeFilenames: "Dateinamen einbeziehen",
SettingIncludeFilenamesDescription: "Wenn aktiviert, werden die Dateinamen in die Ausgabedatei aufgenommen.",
}, },
en: { en: {
MergeFolder: "Merge folder", MergeFolder: "Merge folder",
@ -66,6 +70,8 @@ export const TRANSLATIONS: Record<string, Translation> = {
SettingRemoveYamlProperties: "Remove YAML properties", SettingRemoveYamlProperties: "Remove YAML properties",
SettingRemoveYamlPropertiesDescription: SettingRemoveYamlPropertiesDescription:
"Removes YAML properties from the top of the file during the merge. WARNING: This might unintentionally remove non yaml parts.", "Removes YAML properties from the top of the file during the merge. WARNING: This might unintentionally remove non yaml parts.",
SettingIncludeFilenames: "Include filenames",
SettingIncludeFilenamesDescription: "If enabled, filenames will be included in the output file.",
}, },
fi: { fi: {
MergeFolder: "Yhdistä kansio", MergeFolder: "Yhdistä kansio",
@ -89,6 +95,8 @@ export const TRANSLATIONS: Record<string, Translation> = {
SettingRemoveYamlProperties: "Poista YAML-osio", SettingRemoveYamlProperties: "Poista YAML-osio",
SettingRemoveYamlPropertiesDescription: SettingRemoveYamlPropertiesDescription:
"Poistaa YAML-osio tiedoston yläosasta yhdistämisen aikana. VAROITUS: Tämä voi vahingossa poistaa ei-YAML-osia.", "Poistaa YAML-osio tiedoston yläosasta yhdistämisen aikana. VAROITUS: Tämä voi vahingossa poistaa ei-YAML-osia.",
SettingIncludeFilenames: "Sisällytä tiedostonimet",
SettingIncludeFilenamesDescription: "Jos käytössä, tiedostonimet sisällytetään tulostiedostoon.",
}, },
fr: { fr: {
MergeFolder: "Fusionner le dossier", MergeFolder: "Fusionner le dossier",
@ -113,6 +121,8 @@ export const TRANSLATIONS: Record<string, Translation> = {
SettingRemoveYamlProperties: "Supprimer les propriétés YAML", SettingRemoveYamlProperties: "Supprimer les propriétés YAML",
SettingRemoveYamlPropertiesDescription: SettingRemoveYamlPropertiesDescription:
"Supprime les propriétés YAML du haut du fichier lors de la fusion. ATTENTION : Cela pourrait supprimer accidentellement des parties non YAML.", "Supprime les propriétés YAML du haut du fichier lors de la fusion. ATTENTION : Cela pourrait supprimer accidentellement des parties non YAML.",
SettingIncludeFilenames: "Inclure les noms de fichiers",
SettingIncludeFilenamesDescription: "Si activé, les noms de fichiers seront inclus dans le fichier de sortie.",
}, },
ru: { ru: {
MergeFolder: "Объединить папку", MergeFolder: "Объединить папку",
@ -136,6 +146,8 @@ export const TRANSLATIONS: Record<string, Translation> = {
SettingRemoveYamlProperties: "Удалить свойства YAML", SettingRemoveYamlProperties: "Удалить свойства YAML",
SettingRemoveYamlPropertiesDescription: SettingRemoveYamlPropertiesDescription:
"Удаляет свойства YAML из верхней части файла во время объединения. ВНИМАНИЕ: Это может случайно удалить не-YAML части.", "Удаляет свойства YAML из верхней части файла во время объединения. ВНИМАНИЕ: Это может случайно удалить не-YAML части.",
SettingIncludeFilenames: "Включать имена файлов",
SettingIncludeFilenamesDescription: "Если включено, имена файлов будут включены в выходной файл.",
}, },
ua: { ua: {
MergeFolder: "Об'єднати папку", MergeFolder: "Об'єднати папку",
@ -159,6 +171,8 @@ export const TRANSLATIONS: Record<string, Translation> = {
SettingRemoveYamlProperties: "Видалити властивості YAML", SettingRemoveYamlProperties: "Видалити властивості YAML",
SettingRemoveYamlPropertiesDescription: SettingRemoveYamlPropertiesDescription:
"Видаляє властивості YAML із верхньої частини файлу під час об'єднання. УВАГА: Це може випадково видалити не-YAML частини.", "Видаляє властивості YAML із верхньої частини файлу під час об'єднання. УВАГА: Це може випадково видалити не-YAML частини.",
SettingIncludeFilenames: "Включити імена файлів",
SettingIncludeFilenamesDescription: "Якщо ввімкнено, імена файлів будуть включені у вихідний файл.",
}, },
}; };
@ -170,7 +184,7 @@ export class AdvancedMergeTranslation {
* @constructor * @constructor
*/ */
constructor() { constructor() {
this.language = !Object.keys(TRANSLATIONS).contains(navigator.language) this.language = !Object.keys(TRANSLATIONS).includes(navigator.language)
? DEFAULT_LANGUAGE ? DEFAULT_LANGUAGE
: navigator.language; : navigator.language;
} }