Skip to content

Commit

Permalink
fix: add tests for image path encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTowles committed Sep 26, 2022
1 parent e790ae2 commit 472c0c6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 20 deletions.
24 changes: 16 additions & 8 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import { ensureFileAndGetItsDirectory } from "./folderUtil";

export interface Configuration {

readonly encodePathConfig: string;

readonly defaultImageName: string;
readonly imageFolderPath: string;


readonly insertPatternConfig: string;
readonly showFilePathConfirmInputBox: boolean,
readonly filePathConfirmInputBoxMode: FilePathConfirmInputBoxModeEnum;
readonly encodePath: EncodePathEnum;


readonly defaultImageName: string;
readonly imageFolderPath: string;


// used to prefix and suffix the image name
readonly imageNamePrefix: string;
readonly imageNameSuffix: string;
Expand All @@ -28,11 +30,17 @@ export interface Configuration {

}

export enum FilePathConfirmInputBoxModeEnum {
export enum FilePathConfirmInputBoxModeEnum {
ONLY_NAME = "onlyName",
FULL_PATH = "fullPath",
}

export enum EncodePathEnum {
None = "none",
UrlEncode = "urlEncode",
UrlEncodeSpace = "urlEncodeSpace"
}

export const parseConfigurationToConfig = async ({ projectRootDirPath, editorOpenFilePath, configuration }: { projectRootDirPath: string; editorOpenFilePath: string; configuration: WorkspaceConfiguration }): Promise<Configuration> => {

const editorOpenFolderPath = await ensureFileAndGetItsDirectory(editorOpenFilePath)
Expand All @@ -41,9 +49,9 @@ export const parseConfigurationToConfig = async ({ projectRootDirPath, editorOpe
// Luxon Values used in ImageName = https://moment.github.io/luxon/#/formatting?id=table-of-tokens
let defaultImageName = configuration.get<string>(Constants.Config_DefaultImageName, "yyyy-LL-dd-HH-mm-ss");
let imageFolderPath = configuration.get<string>(Constants.Config_ImageFolderPath, "${currentFileDir}");


let encodePathConfig = configuration.get<string>(Constants.Config_EncodePath, '');
let encodePath = configuration.get<EncodePathEnum>(Constants.Config_EncodePath, EncodePathEnum.UrlEncodeSpace);
let insertPatternConfig = configuration.get<string>(Constants.Config_InsertPattern, '');


Expand Down Expand Up @@ -75,7 +83,7 @@ export const parseConfigurationToConfig = async ({ projectRootDirPath, editorOpe
defaultImageName: defaultImageName,
imageFolderPath: imageFolderPath,

encodePathConfig: encodePathConfig,
encodePath: encodePath,

insertPatternConfig: insertPatternConfig,
showFilePathConfirmInputBox: showFilePathConfirmInputBox,
Expand Down
3 changes: 0 additions & 3 deletions src/folderUtil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import * as fse from 'fs-extra';

describe('FolderUtil', () => {


describe('createImageDirWithImagePath', () => {



it('valid existing folder 01', async () => {

const testPath = path.join(__dirname, 'notRealFile.png').toString();
Expand Down
53 changes: 50 additions & 3 deletions src/renderTextWithImagePath.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, expect, it } from 'vitest'
import * as path from 'path';
import util from 'util';
import { getRelativePathFromEditorFile, renderTextWithImagePath } from './renderTextWithImagePath';
import { encodeImagePath, getRelativePathFromEditorFile, renderTextWithImagePath } from './renderTextWithImagePath';
import { MockWorkspaceConfiguration } from './test/mockWorkspaceConfiguration';
import { Configuration, parseConfigurationToConfig } from './configuration';
import { Configuration, EncodePathEnum, parseConfigurationToConfig } from './configuration';
import { MockLogger } from './test/mockLogger';
import { config } from 'process';

Expand All @@ -23,7 +23,6 @@ describe('renderInsertTextWithImagePath', () => {

const mockLogger = new MockLogger();


it('getRelativePathFromEditorFile - when editor and image same folders', async () => {
const imagePath = path.join(projectRootDir, 'playground', 'test-icon-root.png')
const editorOpenFolderPath = path.join(projectRootDir, 'playground')
Expand Down Expand Up @@ -110,4 +109,52 @@ describe('renderInsertTextWithImagePath', () => {
})).toBe('![](../test-icon-root.png)')
})

it('encodeImagePath - UrlEncodeSpace without spaces', async () => {
const imagePath = path.join(projectRootDir, 'playground', 'test-icon-root.png')
const result = encodeImagePath({imageFilePath: imagePath, encodePath: EncodePathEnum.UrlEncodeSpace})

expect(result).toContain('test-icon-root.png')
})

it('encodeImagePath - UrlEncodeSpace with spaces', async () => {
const imagePath = path.join(projectRootDir, 'playground', 'test icon root.png')
const result = encodeImagePath({imageFilePath: imagePath, encodePath: EncodePathEnum.UrlEncodeSpace})

expect(result).toContain('test%20icon%20root.png')
})


it('encodeImagePath - UrlEncode without spaces', async () => {
const imagePath = path.join(projectRootDir, 'playground', 'test-icon-root.png')
const result = encodeImagePath({imageFilePath: imagePath, encodePath: EncodePathEnum.UrlEncode})

expect(result).toContain('test-icon-root.png')
})

it('encodeImagePath - UrlEncode with spaces', async () => {
const imagePath = path.join(projectRootDir, 'playground', 'test icon root.png')
const result = encodeImagePath({imageFilePath: imagePath, encodePath: EncodePathEnum.UrlEncode})

expect(result).toContain('test%20icon%20root.png')
})
it('encodeImagePath - UrlEncode with special chars', async () => {
const imagePath = path.join(projectRootDir, 'playground', 'image-file-with-special-chars-шеллы.png')
const result = encodeImagePath({imageFilePath: imagePath, encodePath: EncodePathEnum.UrlEncode})

expect(result).toContain('image-file-with-special-chars-%D1%88%D0%B5%D0%BB%D0%BB%D1%8B.png')
})

it('encodeImagePath - None without spaces', async () => {
const imagePath = path.join(projectRootDir, 'playground', 'test-icon-root.png')
const result = encodeImagePath({imageFilePath: imagePath, encodePath: EncodePathEnum.None})

expect(result).toContain('test-icon-root.png')
})
it('encodeImagePath - None with spaces', async () => {
const imagePath = path.join(projectRootDir, 'playground', 'test icon root.png')
const result = encodeImagePath({imageFilePath: imagePath, encodePath: EncodePathEnum.None})

expect(result).toContain('test icon root.png')
})

})
18 changes: 12 additions & 6 deletions src/renderTextWithImagePath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Configuration } from "./configuration";
import { Configuration, EncodePathEnum } from "./configuration";
import * as path from 'path';
import * as upath from 'upath';
import { ILogger } from './logger';
Expand Down Expand Up @@ -54,11 +54,7 @@ export const renderTextWithImagePath = async ({ languageId, config, imageFilePat

logger.debug(`imageFilePath Uri Pre & Suf = ${imageFilePath}`);

if (config.encodePathConfig == "urlEncode") {
imageFilePath = encodeURI(imageFilePath)
} else if (config.encodePathConfig == "urlEncodeSpace") {
imageFilePath = imageFilePath.replace(/ /g, "%20");
}
imageFilePath = encodeImagePath({ imageFilePath, encodePath: config.encodePath });

let imageSyntaxPrefix = "";
let imageSyntaxSuffix = ""
Expand All @@ -85,3 +81,13 @@ export const renderTextWithImagePath = async ({ languageId, config, imageFilePat
logger.debug('renderFilePath end');
return result;
}


export const encodeImagePath = ({ imageFilePath, encodePath}: { imageFilePath: string; encodePath: EncodePathEnum; }): string => {
if (encodePath === EncodePathEnum.UrlEncode) {
imageFilePath = encodeURI(imageFilePath)
} else if (encodePath === EncodePathEnum.UrlEncodeSpace) {
imageFilePath = imageFilePath.replace(/ /g, "%20");
}
return imageFilePath;
}

0 comments on commit 472c0c6

Please sign in to comment.