Skip to content

Commit

Permalink
wip: add nestedNotebookSeparator & support dir & support cmd args
Browse files Browse the repository at this point in the history
  • Loading branch information
ipcjs committed Apr 11, 2024
1 parent a17d7c3 commit 4b82b2e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/YarleOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { CharacterMap } from 'CharacterMap';
import { ImageSizeFormat } from 'image-size-format';

export interface YarleOptions {
enexDir?: string; // used by command line
enexSources?: Array<string>; // used by the UI
templateFile?: string;
currentTemplate?: string;
Expand Down Expand Up @@ -49,6 +48,7 @@ export interface YarleOptions {
monospaceIsCodeBlock?: boolean;
dateFormat?: string;
nestedTags?: TagSeparatorReplaceOptions;
nestedNotebookSeparator?: string;
imageSizeFormat?: ImageSizeFormat;
keepImageSize?: boolean;
keepOriginalAmountOfNewlines?: boolean;
Expand Down
30 changes: 19 additions & 11 deletions src/dropTheRopeRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import * as fs from 'fs';
import * as path from 'path';
import { omit } from 'lodash';

import * as yarle from './yarle';
import { YarleOptions } from './YarleOptions';
import { loggerInfo } from './utils/loggerInfo';
import { clearLogFile } from './utils/clearLogFile';
import { applyLinks } from './utils/apply-links';
import { LanguageFactory } from './outputLanguages/LanguageFactory';
import { recursiveReaddirSync } from './utils/file-utils';

export const run = async (opts?: YarleOptions) => {
clearLogFile();
Expand All @@ -20,19 +22,25 @@ export const run = async (opts?: YarleOptions) => {
? argv['configFile']
: `${process.cwd()}/${argv['configFile']}`
: `${__dirname}/../config.json`;
const argOptions = omit(argv, 'configFile', '_')
if (typeof argOptions.enexSources === 'string') {
argOptions.enexSources = [argOptions.enexSources]
}
console.log(`Loading config from ${configFile}`);
const options: YarleOptions = {...require(configFile), ...opts};
if (options.enexSources.length === 1 && options.enexSources[0].endsWith('.enex')) {
loggerInfo(`Converting notes in file: ${options.enexSources}`);
} else {
const enexFiles = fs
.readdirSync(options.enexSources[0])
.filter((file: any) => {
return file.match(/.*\.enex/ig);
});
const options: YarleOptions = { ...require(configFile), ...argOptions, ...opts };

const isEnexFile = (path: string) => path.toLowerCase().endsWith('.enex')
options.enexSources = options.enexSources.reduce((arr, source) => {
if (isEnexFile(source)) {
arr.push(source)
} else {
arr.push(...[...recursiveReaddirSync(source)]
.filter(isEnexFile))
}
return arr
}, [] as string[])
loggerInfo(`Converting notes in files: ${options.enexSources}`);

options.enexSources = enexFiles.map(enexFile => `${options.enexSources[0]}${path.sep}${enexFile}`);
}
const outputNotebookFolders = await yarle.dropTheRope(options);

// POSTPROCESSES
Expand Down
13 changes: 13 additions & 0 deletions src/utils/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { yarleOptions } from './../yarle';
import { isLogseqJournal } from './is-logseq-journal';
import {getCreationTime} from './content-utils'
import { EvernoteNoteData } from './../models';
import { resolve } from 'path';

export const writeFile = (absFilePath: string, noteContent: string, pureNoteData: EvernoteNoteData): void => {
try {
Expand Down Expand Up @@ -37,4 +38,16 @@ export const writeFile = (absFilePath: string, noteContent: string, pureNoteData
console.log(`replaced output written to: ${filePath}`);
fs.writeFileSync(filePath, updatedContent);
setFileDates(filePath, birthtime, mtime)
}

export function* recursiveReaddirSync(dir: string): Generator<string> {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
for (const dirent of dirents) {
const res = resolve(dir, dirent.name);
if (dirent.isDirectory()) {
yield* recursiveReaddirSync(res);
} else {
yield res;
}
}
}
6 changes: 4 additions & 2 deletions src/utils/filename-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ export const getNoteName = (dstPath: string, note: EvernoteNoteData): string =>
};

export const getNotebookName = (enexFile: string): string => {
const notebookName = normalizeFilenameString(path.basename(enexFile, '.enex'));

let notebookName = normalizeFilenameString(path.basename(enexFile, '.enex'));
if (yarleOptions.nestedNotebookSeparator) {
notebookName = notebookName.replace(yarleOptions.nestedNotebookSeparator, path.sep)
}
return notebookName;
};

Expand Down
3 changes: 3 additions & 0 deletions src/utils/folder-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ export const setPaths = (enexSource: string): void => {
// loggerInfo(`enex folder split: ${JSON.stringify(enexFolder)}`);
let enexFile = (enexFolder.length >= 1 ? enexFolder[enexFolder.length - 1] : enexFolder[0]).split(/.enex$/)[0];
enexFile = normalizeFilenameString(enexFile);
if (yarleOptions.nestedNotebookSeparator) {
enexFile = enexFile.replace(yarleOptions.nestedNotebookSeparator, path.sep)
}
// loggerInfo(`enex file: ${enexFile}`);

const outputDir = path.isAbsolute(yarleOptions.outputDir)
Expand Down

0 comments on commit 4b82b2e

Please sign in to comment.