generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {MkdocsPublicationSettings} from "../settings/interface"; | ||
import {MetadataCache, TFile} from "obsidian"; | ||
import {getReceiptFolder, createRelativePath, getImageLinkOptions} from "./filePathConvertor"; | ||
|
||
function convertWikilinks(fileContent: string, settings: MkdocsPublicationSettings, linkedFiles: {linked: TFile, linkFrom: string, altText: string}[]) { | ||
if (!settings.convertWikiLinks) { | ||
return fileContent; | ||
} | ||
const wikiRegex = /\[\[.*?\]\]/g; | ||
const wikiMatches = fileContent.match(wikiRegex); | ||
if (wikiMatches) { | ||
const fileRegex = /(?<=\[\[).*?(?=([\]|]))/; | ||
for (const wikiMatch of wikiMatches) { | ||
const fileMatch = wikiMatch.match(fileRegex); | ||
if (fileMatch) { | ||
const fileName = fileMatch[0]; | ||
const linkedFile=linkedFiles.find(item => item.linkFrom===fileName); | ||
if (linkedFile) { | ||
const altText = linkedFile.altText.length > 0 ? linkedFile.altText : linkedFile.linked.extension === 'md' ? linkedFile.linked.basename : ""; | ||
const linkCreator = `[${altText}](${encodeURI(linkedFile.linkFrom)})`; | ||
fileContent = fileContent.replace(wikiMatch, linkCreator); | ||
} else if (!fileName.startsWith('http')) { | ||
const altMatch = wikiMatch.match(/(?<=\|).*(?=]])/); | ||
const altCreator = fileName.split('/'); | ||
const altLink = creatorAltLink(altMatch, altCreator, fileName.split('.').at(-1)); | ||
const linkCreator = `[${altLink}](${encodeURI(fileName.trim())})`; | ||
fileContent = fileContent.replace(wikiMatch, linkCreator); | ||
} | ||
} | ||
} | ||
} | ||
return fileContent; | ||
} | ||
|
||
function convertLinkCitation(fileContent: string, settings: MkdocsPublicationSettings, linkedFiles : {linked: TFile, linkFrom: string, altText: string}[], metadataCache: MetadataCache, sourceFile: TFile) { | ||
if (!settings.convertForGithub) { | ||
return fileContent; | ||
} | ||
for (const linkedFile of linkedFiles) { | ||
let pathInGithub=linkedFile.linked.extension === 'md' ? getReceiptFolder(linkedFile.linked, settings, metadataCache) : getImageLinkOptions(linkedFile.linked, settings); | ||
const sourcePath = getReceiptFolder(sourceFile, settings, metadataCache); | ||
pathInGithub = createRelativePath(sourcePath, pathInGithub); | ||
const regexToReplace = new RegExp(`(\\[{2}.*${linkedFile.linkFrom}.*\\]{2})|(\\[.*\\]\\(.*${linkedFile.linkFrom}.*\\))`, 'g'); | ||
const matchedLink = fileContent.match(regexToReplace); | ||
if (matchedLink) { | ||
for (const link of matchedLink) { | ||
const newLink = link.replace(linkedFile.linkFrom, pathInGithub); | ||
fileContent = fileContent.replace(link, newLink); | ||
} | ||
} | ||
} | ||
return fileContent; | ||
} | ||
|
||
function creatorAltLink(altMatch: RegExpMatchArray, altCreator: string[], fileExtension: string) { | ||
if (altMatch) { | ||
return altMatch[0] | ||
} | ||
if (fileExtension === 'md') { | ||
return altCreator.length > 1 ? altCreator[altCreator.length-1] : altCreator[0] //alt text based on filename for markdown files | ||
} | ||
return '' | ||
} | ||
|
||
|
||
export {convertWikilinks, convertLinkCitation, creatorAltLink}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {MetadataCache, TFile} from "obsidian"; | ||
import {MkdocsPublicationSettings} from "../settings/interface"; | ||
|
||
function createRelativePath(sourcePath: string, targetPath: string) { | ||
const sourceList = sourcePath.split('/'); | ||
const targetList = targetPath.split('/'); | ||
const diffSourcePath = sourceList.filter(x => !targetList.includes(x)); | ||
const diffTargetPath = targetList.filter(x => !sourceList.includes(x)); | ||
const diffTarget = function (folderPath: string[]) { | ||
const relativePath = []; | ||
for (const folder of folderPath) { | ||
if (folder != folderPath.at(-1)) { | ||
relativePath.push('..'); | ||
} | ||
} | ||
return relativePath; | ||
}; | ||
return diffTarget(diffSourcePath).concat(diffTargetPath).join('/') | ||
} | ||
|
||
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 ? '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 | ||
path = folderDefault + '/' + file.path.replace(file.name, fileName); | ||
} | ||
return path | ||
} | ||
} | ||
|
||
function getImageLinkOptions(file: TFile, settings: MkdocsPublicationSettings) { | ||
let fileDefaultPath = file.path; | ||
const fileName = file.name; | ||
if (settings.defaultImageFolder.length > 0) { | ||
fileDefaultPath = settings.defaultImageFolder + "/" + fileName; | ||
} else if (settings.folderDefaultName.length > 0) { | ||
fileDefaultPath = settings.folderDefaultName + "/" + fileName; | ||
} | ||
return fileDefaultPath; | ||
} | ||
|
||
export { | ||
getReceiptFolder, | ||
getImageLinkOptions, | ||
createRelativePath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters