Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: notename in resources #221

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

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

1 change: 1 addition & 0 deletions src/YarleOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export interface YarleOptions {
nestedTags?: TagSeparatorReplaceOptions;
keepImageSize?: OutputFormat;
keepOriginalAmountOfNewlines?: boolean;
useNoteNameAsAttachmentName?: boolean;
}
10 changes: 5 additions & 5 deletions src/process-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ export const processResources = (note: any): string => {

const relativeResourceWorkDir = utils.getRelativeResourceDir(note);
const absoluteResourceWorkDir = utils.getAbsoluteResourceDir(note);

const noteName = utils.getNoteNameByMdPath(note);
utils.clearResourceDir(note);
if (Array.isArray(note.resource)) {
for (const resource of note.resource) {
resourceHashes = {
...resourceHashes,
...processResource(absoluteResourceWorkDir, resource)};
...processResource(noteName, absoluteResourceWorkDir, resource)};
}
} else {
resourceHashes = {
...resourceHashes,
...processResource(absoluteResourceWorkDir, note.resource)};
...processResource(noteName, absoluteResourceWorkDir, note.resource)};
}

for (const hash of Object.keys(resourceHashes)) {
Expand Down Expand Up @@ -56,11 +56,11 @@ const addMediaReference = (content: string, resourceHashes: any, hash: any, work
return updatedContent;
};

const processResource = (workDir: string, resource: any): any => {
const processResource = (noteName: string, workDir: string, resource: any): any => {
const resourceHash: any = {};
const data = resource.data.$text;

const resourceFileProps = utils.getResourceFileProperties(workDir, resource);
const resourceFileProps = utils.getResourceFileProperties(noteName, workDir, resource);
const fileName = resourceFileProps.fileName;
const absFilePath = `${workDir}/${fileName}`;

Expand Down
14 changes: 9 additions & 5 deletions src/utils/filename-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@ export const getFileIndex = (dstPath: string, fileNamePrefix: string): number |
return index;

};
export const getResourceFileProperties = (workDir: string, resource: any): ResourceFileProperties => {
export const getResourceFileProperties = (noteName: string, workDir: string, resource: any): ResourceFileProperties => {
const UNKNOWNFILENAME = 'unknown_filename';

const extension = getExtension(resource);
let fileName = UNKNOWNFILENAME;

if (resource['resource-attributes'] && resource['resource-attributes']['file-name']) {
const fileNamePrefix = resource['resource-attributes']['file-name'].substr(0, 50);
if (yarleOptions.useNoteNameAsAttachmentName){
fileName = noteName;
}
else{
if (resource['resource-attributes'] && resource['resource-attributes']['file-name']) {
const fileNamePrefix = resource['resource-attributes']['file-name'].substr(0, 50);

fileName = fileNamePrefix.split('.')[0];
fileName = fileNamePrefix.split('.')[0];

}
}

const index = getFileIndex(workDir, fileName);
const fileNameWithIndex = index > 0 ? `${fileName}.${index}` : fileName;

Expand Down
13 changes: 9 additions & 4 deletions src/utils/folder-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ import { Path } from '../paths';
import { yarleOptions } from '../yarle';

import { getNoteFileName, getNoteName } from './filename-utils';
import { NoteData } from 'models';

export const paths: Path = {};

export const getResourceDir = (dstPath: string, note: any): string => {
return getNoteName(dstPath, note).replace(/\s/g, '_');
export const getResourceDir = (note: any): string => {
return getNoteName(paths.mdPath, note).replace(/\s/g, '_');
};

const getFilePath = (dstPath: string, note: any): string => {
return `${dstPath}/${getNoteFileName(dstPath, note)}`;
};

export const getNoteNameByMdPath = (note: any): string => {
return getNoteName(paths.mdPath, note);
}

export const getMdFilePath = (note: any): string => {
return getFilePath(paths.mdPath, note);
};
Expand All @@ -38,11 +43,11 @@ const clearDistDir = (dstPath: string): void => {
};

export const getRelativeResourceDir = (note: any): string => {
return yarleOptions.haveEnexLevelResources ? './_resources' : `./_resources/${getResourceDir(paths.mdPath, note)}.resources`;
return yarleOptions.haveEnexLevelResources ? './_resources' : `./_resources/${getResourceDir(note)}.resources`;
};

export const getAbsoluteResourceDir = (note: any): string => {
return yarleOptions.haveEnexLevelResources ? paths.resourcePath : `${paths.resourcePath}/${getResourceDir(paths.mdPath, note)}.resources`;
return yarleOptions.haveEnexLevelResources ? paths.resourcePath : `${paths.resourcePath}/${getResourceDir(note)}.resources`;
};

const resourceDirClears = new Map<string, number>();
Expand Down