Skip to content

Commit

Permalink
feat: adding subfolder sync
Browse files Browse the repository at this point in the history
close #9
  • Loading branch information
Mara-Li committed Jun 18, 2022
1 parent 704c098 commit 117c200
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions mkdocsPublisher/utils/filePathConvertor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {MetadataCache, TFile} from "obsidian";
import {MkdocsPublicationSettings} from "../settings/interface";
import {folderSettings, MkdocsPublicationSettings} from "../settings/interface";

function createRelativePath(sourcePath: string, targetPath: string) {
const sourceList = sourcePath.split('/');
Expand All @@ -18,33 +18,59 @@ function createRelativePath(sourcePath: string, targetPath: string) {
return diffTarget(diffSourcePath).concat(diffTargetPath).join('/')
}

function createObsidianPath(file: TFile, settings:MkdocsPublicationSettings) {
/**
* Create link path based on settings and file path
* @param file : TFile - Image TFile
* @param settings : MkdocsPublicationSettings - Settings
* @returns string - Link path
*/
const folderDefault = settings.folderDefaultName;
const fileName = file.name.replace('.md', '') === file.parent.name && settings.folderNote ? 'index.md' : file.name
const rootFolder = folderDefault.length > 0 ? folderDefault + "/" : ''
const path = rootFolder + file.path.replace(file.name, fileName);
if (settings.subFolder.length > 0) {
return path.replace(settings.subFolder + '/', '');
}
return path;
}

function createFrontmatterPath(file: TFile, settings: MkdocsPublicationSettings, metadataCache: MetadataCache) {
let path = settings.folderDefaultName.length > 0 ? settings.folderDefaultName + "/" + file.name : file.name;
const frontmatter = metadataCache.getCache(file.path).frontmatter
let folderRoot = settings.rootFolder;
if (folderRoot.length > 0) {
folderRoot = folderRoot + "/";
}
if (frontmatter && frontmatter[settings.yamlFolderKey]) {
const category = frontmatter[settings.yamlFolderKey]
const parentCatFolder = !category.endsWith('/') ? category.split('/').at(-1): category.split('/').at(-2);
const fileName = settings.folderNote && parentCatFolder === file.name.replace('.md', '') ? 'index.md' : file.name
path = folderRoot + frontmatter[settings.yamlFolderKey] + "/" + fileName;
}
return path
}

function getReceiptFolder(file: TFile, settings:MkdocsPublicationSettings, metadataCache: MetadataCache) {
if (file.extension === 'md') {
const folderDefault = settings.folderDefaultName;
let path = settings.folderDefaultName.length > 0 ? settings.folderDefaultName + "/" + file.name : file.name;

if (settings.downloadedFolder === "yamlFrontmatter") {
const frontmatter = metadataCache.getCache(file.path).frontmatter
let folderRoot = settings.rootFolder;
if (folderRoot.length > 0) {
folderRoot = folderRoot + "/";
}
if (frontmatter && frontmatter[settings.yamlFolderKey]) {
const category = frontmatter[settings.yamlFolderKey]
const parentCatFolder = !category.endsWith('/') ? category.split('/').at(-1): category.split('/').at(-2);
const fileName = settings.folderNote && parentCatFolder === file.name.replace('.md', '') ? 'index.md' : file.name
path = folderRoot + frontmatter[settings.yamlFolderKey] + "/" + fileName;
}
} else if (settings.downloadedFolder === "obsidianPath") {
const fileName = file.name.replace('.md', '') === file.parent.name && settings.folderNote ? 'index.md' : file.name
const rootFolder = folderDefault.length > 0 ? folderDefault + "/" : ''
path = rootFolder + file.path.replace(file.name, fileName);
if (settings.downloadedFolder === folderSettings.yaml) {
path = createFrontmatterPath(file, settings, metadataCache)
} else if (settings.downloadedFolder === folderSettings.obsidian) {
path = createObsidianPath(file, settings)
}
return path
}
}

function getImageLinkOptions(file: TFile, settings: MkdocsPublicationSettings) {
/**
* Create link path based on settings and file path
* @param file : TFile - Image TFile
* @param settings : MkdocsPublicationSettings - Settings
* @returns string - Link path
*/
let fileDefaultPath = file.path;
const fileName = file.name;
if (settings.defaultImageFolder.length > 0) {
Expand Down

0 comments on commit 117c200

Please sign in to comment.