Skip to content

Commit

Permalink
fix: attemps to fix image relative creator
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jul 7, 2022
1 parent 59bc388 commit 68a00b8
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
9 changes: 9 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
# and commit this file to your remote git repository to share the goodness with others.

tasks:
- init: npm install && npm run build
command: npm run dev


2 changes: 1 addition & 1 deletion manifest-beta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"author": "Mara-Li",
"authorUrl": "https://github.com/Mara-Li",
"isDesktopOnly": false
}
}
16 changes: 13 additions & 3 deletions mkdocsPublisher/githubInteraction/filesManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export class FilesManagement extends MkdocsPublish {
}

getLinkedImageAndFiles(file: TFile) {
/**
* Create a database with every internal links and embeded image and files
* @param file: the source file
* @return linkedFiles: array of linked files
*/
const linkedFiles = this.getLinkedFiles(file);
const imageEmbedded = this.metadataCache.getFileCache(file).embeds;
if (imageEmbedded != undefined) {
Expand All @@ -80,9 +85,9 @@ export class FilesManagement extends MkdocsPublish {
const imageExt = imageLink.extension;
if (imageExt.match(/(png|jpe?g|svg|bmp|gif|md)$/i)) {
linkedFiles.push({
'linked': imageLink,
'linkFrom': image.link,
'altText': image.displayText
'linked': imageLink, //TFile found
'linkFrom': image.link, //path of the founded file
'altText': image.displayText //alt text if exists, filename otherwise
})

}
Expand All @@ -95,6 +100,11 @@ export class FilesManagement extends MkdocsPublish {
}

getLinkedFiles(file: TFile): { linked: TFile, linkFrom: string, altText: string }[] {
/**
* Create an objet of all files embedded in the shared files
* @param file: The file shared
* @return the file linked (Tfile), the path to it, and the alt text if exists
*/
const embedCaches = this.metadataCache.getCache(file.path).links;
const embedList = [];
if (embedCaches != undefined) {
Expand Down
13 changes: 11 additions & 2 deletions mkdocsPublisher/utils/convertText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ function convertWikilinks(fileContent: string, settings: MkdocsPublicationSettin
}

function convertLinkCitation(fileContent: string, settings: MkdocsPublicationSettings, linkedFiles : {linked: TFile, linkFrom: string, altText: string}[], metadataCache: MetadataCache, sourceFile: TFile) {
/**
* Convert internal links with changing the path to the relative path in the github repository
* @param fileContent: The file content
* @param settings: Settings of the plugins
* @param linkedFiles: A list of linked files including the linked file in TFile format and the linked file (string) including the alt text
* @param metadataCache: Metadata cache
* @param sourceFile: The original file
* @return the file contents with converted internal links
*/
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);
pathInGithub = createRelativePath(sourceFile, pathInGithub, metadataCache, settings).replace('.md', '');
const pathInGithub = createRelativePath(sourceFile, linkedFile.linked, metadataCache, settings).replace('.md', '');
const regexToReplace = new RegExp(`(\\[{2}${linkedFile.linkFrom}(\\|.*)?\\]{2})|(\\[.*\\]\\(${linkedFile.linkFrom}\\))`, 'g');
const matchedLink = fileContent.match(regexToReplace);
if (matchedLink) {
Expand Down
15 changes: 12 additions & 3 deletions mkdocsPublisher/utils/filePathConvertor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import {MetadataCache, TFile} from "obsidian";
import {folderSettings, MkdocsPublicationSettings} from "../settings/interface";

function createRelativePath(sourceFile: TFile, targetPath: string, metadata: MetadataCache, settings: MkdocsPublicationSettings) {
function createRelativePath(sourceFile: TFile, targetFile: TFile, metadata: MetadataCache, settings: MkdocsPublicationSettings) {
/**
* Create relative path from a sourceFile to a targetPath. If the target file is a note, only share if the frontmatter sharekey is present and true
* @param sourceFile: TFile, the shared file containing all links, embed etc
* @param targetFile: Tfile, the targeted file
* @param settings: MkdocsPublicationSettings
* @param metadata: metadataCache
* @return string : relative created path
*/
const sourcePath = getReceiptFolder(sourceFile, settings, metadata);
const frontmatter = metadata.getCache(sourcePath)? metadata.getCache(sourcePath).frontmatter : null;
if (!frontmatter || !frontmatter[settings.shareKey]) {
const frontmatter = metadata.getCache(targetFile.path) ? metadata.getCache(targetFile.path).frontmatter : null;
if (targetFile.extension === '.md' && (!frontmatter || !frontmatter[settings.shareKey])) {
return sourceFile.name;
}
const targetPath = targetFile.extension === 'md' ? getReceiptFolder(targetFile, settings, metadata) : getImageLinkOptions(targetFile, settings);
const sourceList = sourcePath.split('/');
const targetList = targetPath.split('/');
const diffSourcePath = sourceList.filter(x => !targetList.includes(x));
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 68a00b8

Please sign in to comment.