Skip to content

Commit

Permalink
feat: add 2 new frontmatter key for converting internal links
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Dec 8, 2022
1 parent 1ea93b7 commit a8d4a89
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 36 deletions.
14 changes: 9 additions & 5 deletions plugin/contents_conversion/convertLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { FrontMatterCache, MetadataCache, TFile, Vault } from "obsidian";
import {
FrontmatterConvert,
GitHubPublisherSettings,
LinkedNotes, RepoFrontmatter,
LinkedNotes,
RepoFrontmatter,
} from "../settings/interface";
import { createRelativePath } from "./filePathConvertor";
import { isAttachment } from "../src/utils";
Expand Down Expand Up @@ -191,6 +192,7 @@ function addAltText(link: string, linkedFile: LinkedNotes): string {
* @param {Vault} vault The vault
* @param {FrontMatterCache} frontmatter The frontmatter cache
* @param {RepoFrontmatter} sourceRepoFrontmatter The frontmatter of the source file
* @param {FrontmatterConvert} frontmatterSettings The frontmatter settings
* @return {string} the file contents with converted internal links
*/

Expand All @@ -202,9 +204,10 @@ export async function convertLinkCitation(
sourceFile: TFile,
vault: Vault,
frontmatter: FrontMatterCache,
sourceRepoFrontmatter: RepoFrontmatter | RepoFrontmatter[]
sourceRepoFrontmatter: RepoFrontmatter | RepoFrontmatter[],
frontmatterSettings: FrontmatterConvert
): Promise<string> {
if (!settings.convertForGithub) {
if (!frontmatterSettings.convertInternalLinks) {
return fileContent;
}
for (const linkedFile of linkedFiles) {
Expand All @@ -215,8 +218,9 @@ export async function convertLinkCitation(
settings,
vault,
frontmatter,
sourceRepoFrontmatter
)
sourceRepoFrontmatter,
frontmatterSettings
);
pathInGithub = pathInGithub.replace(".md", "");
const regexToReplace = new RegExp(
`(\\[{2}${linkedFile.linkFrom}(\\\\?\\|.*)?\\]{2})|(\\[.*\\]\\(${linkedFile.linkFrom}\\))`,
Expand Down
9 changes: 6 additions & 3 deletions plugin/contents_conversion/convertText.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
FrontmatterConvert,
GitHubPublisherSettings,
LinkedNotes, RepoFrontmatter,
LinkedNotes,
RepoFrontmatter,
} from "../settings/interface";
import {
App,
Expand Down Expand Up @@ -265,7 +266,8 @@ export async function convertDataviewQueries(
sourceFile,
vault,
frontmatter,
sourceFrontmatter
sourceFrontmatter,
frontmatterSettings
);
md = convertWikilinks(
md,
Expand Down Expand Up @@ -344,7 +346,8 @@ export async function mainConverting(
file,
vault,
frontmatter,
sourceRepo
sourceRepo,
frontmatterSettings
);
text = convertWikilinks(text, frontmatterSettings, settings, linkedFiles);
text = findAndReplaceText(text, settings, true);
Expand Down
50 changes: 35 additions & 15 deletions plugin/contents_conversion/filePathConvertor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import {
folderSettings,
LinkedNotes,
GitHubPublisherSettings,
FrontmatterConvert, RepoFrontmatter,
FrontmatterConvert,
RepoFrontmatter,
} from "../settings/interface";
import {checkIfRepoIsInAnother, getFrontmatterCondition, getRepoFrontmatter} from "../src/utils";
import {
checkIfRepoIsInAnother,
getFrontmatterCondition,
getRepoFrontmatter,
} from "../src/utils";

/**
* Get the dataview path from a markdown file
Expand Down Expand Up @@ -55,14 +60,22 @@ function getDataviewPath(

/**
* Vérifie qu'un fichier est partagé et permet la conversion du lien pointant vers lui s'il ne l'est pas
* @param {GitHubPublisherSettings} settings
* @param {string} sharekey
* @param {FrontMatterCache} frontmatter
* @param {FrontmatterConvert} frontmatterSettings
* @return {boolean}
*/
function isShared(settings: GitHubPublisherSettings, frontmatter: FrontMatterCache): boolean {
const shared = frontmatter && frontmatter[settings.shareKey] ? frontmatter[settings.shareKey] : false;
return !!(shared || (!shared && settings.convertInternalNonShared));

function isShared(
sharekey: string,
frontmatter: FrontMatterCache,
frontmatterSettings: FrontmatterConvert
): boolean {
const shared =
frontmatter && frontmatter[sharekey] ? frontmatter[sharekey] : false;
return !!(
shared ||
(!shared && frontmatterSettings.convertInternalNonShared)
);
}

/**
Expand All @@ -74,6 +87,7 @@ function isShared(settings: GitHubPublisherSettings, frontmatter: FrontMatterCac
* @param {Vault} vault Vault
* @param {FrontMatterCache | null} frontmatter FrontmatterCache or null
* @param {RepoFrontmatter[] | RepoFrontmatter} sourceRepo The repoFrontmatter from the original file
* @param {FrontmatterConvert} frontmatterSettings FrontmatterConvert
* @return {string} relative path
*/

Expand All @@ -84,20 +98,26 @@ async function createRelativePath(
settings: GitHubPublisherSettings,
vault: Vault,
frontmatter: FrontMatterCache | null,
sourceRepo: RepoFrontmatter[] | RepoFrontmatter
sourceRepo: RepoFrontmatter[] | RepoFrontmatter,
frontmatterSettings: FrontmatterConvert
): Promise<string> {
const sourcePath = getReceiptFolder(sourceFile, settings, metadata, vault);
const frontmatterTarget = await metadata.getFileCache(targetFile.linked).frontmatter;
const frontmatterTarget = await metadata.getFileCache(targetFile.linked)
.frontmatter;
const targetRepo = await getRepoFrontmatter(settings, frontmatterTarget);
const isFromAnotherRepo = checkIfRepoIsInAnother(sourceRepo, targetRepo);
const shared = isShared(settings, frontmatterTarget);
const shared = isShared(
settings.shareKey,
frontmatter,
frontmatterSettings
);
if (
targetFile.linked.extension === "md"
&&
isFromAnotherRepo === false
|| shared === false
(targetFile.linked.extension === "md" && isFromAnotherRepo === false) ||
shared === false
) {
console.log(`${targetFile.linked.name} is not shared/not to be converted`);
console.log(
`${targetFile.linked.name} is not shared/not to be converted`
);
return targetFile.altText;
}
if (targetFile.linked.path === sourceFile.path) {
Expand Down
2 changes: 2 additions & 0 deletions plugin/settings/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ export interface FrontmatterConvert {
removeEmbed: boolean;
dataview: boolean;
hardbreak: boolean;
convertInternalNonShared: boolean;
convertInternalLinks: boolean;
}

export interface RepoFrontmatter {
Expand Down
38 changes: 25 additions & 13 deletions plugin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ export async function noticeMessage(
repository
);
}

}

/**
Expand Down Expand Up @@ -356,18 +355,25 @@ export function getFrontmatterCondition(
removeEmbed: false,
dataview: settings.convertDataview,
hardbreak: settings.hardBreak,
convertInternalNonShared: settings.convertInternalNonShared,
convertInternalLinks: settings.convertForGithub,
};
if (frontmatter.links !== undefined) {
if (typeof frontmatter.links === "object") {
if (frontmatter.links.convert !== undefined) {
settingsConversion.links = frontmatter.links.convert;
}
if (frontmatter.links.internals !== undefined) {
settingsConversion.links = frontmatter.links.internals;
settingsConversion.convertInternalLinks =
frontmatter.links.internals;
}
if (frontmatter.links.mdlinks !== undefined) {
settingsConversion.convertWiki = frontmatter.links.mdlinks;
}
if (frontmatter.links.nonShared !== undefined) {
settingsConversion.convertInternalNonShared =
frontmatter.links.nonShared;
}
} else {
settingsConversion.links = frontmatter.links;
}
Expand Down Expand Up @@ -414,6 +420,12 @@ export function getFrontmatterCondition(
if (frontmatter.hardbreak !== undefined) {
settingsConversion.hardbreak = frontmatter.hardbreak;
}
if (frontmatter.internals !== undefined) {
settingsConversion.convertInternalLinks = frontmatter.internals;
}
if (frontmatter.nonShared !== undefined) {
settingsConversion.convertInternalNonShared = frontmatter.nonShared;
}
return settingsConversion;
}

Expand Down Expand Up @@ -573,7 +585,7 @@ function repositoryStringSlice(repo: string, repoFrontmatter: RepoFrontmatter) {

export function checkIfRepoIsInAnother(
source: RepoFrontmatter | RepoFrontmatter[],
target: RepoFrontmatter | RepoFrontmatter[],
target: RepoFrontmatter | RepoFrontmatter[]
) {
source = source instanceof Array ? source : [source];
target = target instanceof Array ? target : [target];
Expand All @@ -584,33 +596,33 @@ export function checkIfRepoIsInAnother(
* @param {RepoFrontmatter} target
* @return {boolean}
*/
const isSame= (source: RepoFrontmatter, target: RepoFrontmatter) => {
console.log(source.owner === target.owner, source.repo === target.repo, source.branch === target.branch);
const isSame = (source: RepoFrontmatter, target: RepoFrontmatter) => {
console.log(
source.owner === target.owner,
source.repo === target.repo,
source.branch === target.branch
);
return (
source.owner === target.owner &&
source.repo === target.repo &&
source.branch === target.branch
);
}
};

for (const repoTarget of target) {
for (const repoSource of source) {
if (
isSame(repoTarget, repoSource)
) {
if (isSame(repoTarget, repoSource)) {
return true;
}
}
}
for (const sourceRepo of source) {
for (const targetRepo of target) {
if (
isSame(sourceRepo, targetRepo)
) {
if (isSame(sourceRepo, targetRepo)) {
return true;
}
}
}

return false
return false;
}

0 comments on commit a8d4a89

Please sign in to comment.