Skip to content

Commit

Permalink
fix: truncate if the filepath exceeds the OS limit (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
akosbalasko authored Mar 18, 2023
1 parent 52d2fdf commit 9510fac
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 18 deletions.
5 changes: 5 additions & 0 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"mime-types": "2.1.28",
"minimist": "1.2.6",
"moment": "2.29.4",
"nanoid": "3.3.4",
"proxyquire": "2.1.3",
"rimraf": "3.0.2",
"sanitize-filename": "1.6.3",
Expand Down
1 change: 1 addition & 0 deletions src/models/InternalLink.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface InternalLink {
url: string;
title: string;
uniqueEnd?: string;
}
21 changes: 18 additions & 3 deletions src/runtime-properties.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { InternalLink, NoteData } from './models';
export interface NoteIdNameEntry {
title: string;
noteName: string;
notebookName: string;
uniqueEnd: string;
}

export interface NoteIdNames {
[key: string]: NoteIdNameEntry;
}
export class RuntimePropertiesSingleton {

static instance: RuntimePropertiesSingleton;

noteIdNameMap: any;
noteIdNameTOCMap: any; // Table of Contents map - the trusted source
noteIdNameMap: NoteIdNames;
noteIdNameTOCMap: NoteIdNames; // Table of Contents map - the trusted source
currentNoteName: string;
currentNotebookName: string;
currentNotePath: string;
Expand All @@ -30,6 +39,7 @@ export class RuntimePropertiesSingleton {
title: linkItem.title,
noteName: this.currentNoteName,
notebookName: this.currentNotebookName,
uniqueEnd: linkItem.uniqueEnd,
};
}
addItemToTOCMap(linkItem: InternalLink): void {
Expand All @@ -38,6 +48,7 @@ export class RuntimePropertiesSingleton {
title: linkItem.title,
noteName: this.currentNoteName,
notebookName: this.currentNotebookName,
uniqueEnd: linkItem.uniqueEnd,
};
}

Expand All @@ -49,13 +60,17 @@ export class RuntimePropertiesSingleton {
return this.noteIdNameTOCMap;
}

getAllNoteIdNameMap(): any {
getAllNoteIdNameMap(): NoteIdNames {
return {
...this.noteIdNameMap,
...this.noteIdNameTOCMap,
};
}

getNoteIdNameMapByNoteTitle(noteTitle: string): any {
return Object.values(this.getAllNoteIdNameMap()).filter(noteIdName => noteIdName.title === noteTitle);
}

setCurrentNotebookName(currentNotebookName: string): void {
this.currentNotebookName = currentNotebookName;
}
Expand Down
27 changes: 26 additions & 1 deletion src/utils/apply-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ import * as path from 'path';

import { YarleOptions } from './../YarleOptions';
import { RuntimePropertiesSingleton } from './../runtime-properties';
import { truncatFileName } from './folder-utils';
import { escapeStringRegexp } from './escape-string-regexp';

export const applyLinks = (options: YarleOptions, outputNotebookFolders: Array<string>): void => {
const linkNameMap = RuntimePropertiesSingleton.getInstance();
const allLinks = linkNameMap.getAllNoteIdNameMap();
const allconvertedFiles: Array<string> = [];
for (const outputFolder of outputNotebookFolders){
getAllFiles(outputFolder, allconvertedFiles);
}
for (const [linkName, linkProps] of Object.entries(allLinks)) {
const fileName: string = (linkProps as any)['title'];
const uniqueId = linkProps.uniqueEnd;
let fileName = (linkProps as any)['title'];
if (allconvertedFiles.find(fn => fn.includes(uniqueId))) {
fileName = truncatFileName(fileName, uniqueId);
}

const notebookName: string = (linkProps as any)['notebookName'];
const encodedFileName = options.urlEncodeFileNamesAndLinks ? encodeURI(fileName as string) : fileName as string;

Expand Down Expand Up @@ -44,3 +54,18 @@ export const applyLinks = (options: YarleOptions, outputNotebookFolders: Array<s
}
}
};

const getAllFiles = (dirPath: string, arrayOfFiles: Array<any>): Array<any> => {
const files = fs.readdirSync(dirPath);

arrayOfFiles = arrayOfFiles || [];
files.forEach(file => {
if (fs.statSync(`${dirPath}${path.sep}${file}`).isDirectory()) {
arrayOfFiles = getAllFiles(`${dirPath}${path.sep}${file}`, arrayOfFiles);
} else {
arrayOfFiles.push(path.join(__dirname, dirPath, '/', file));
}
});

return arrayOfFiles;
};
5 changes: 5 additions & 0 deletions src/utils/filename-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as fs from 'fs';
import Moment from 'moment';
import * as path from 'path';
import * as mime from 'mime-types';
import { nanoid } from 'nanoid';

import { yarleOptions } from '../yarle';

Expand Down Expand Up @@ -100,6 +101,10 @@ export const getZettelKastelId = (note: any, dstPath: string): string => {

};

export const getUniqueId = (): string => {
return nanoid(5);
};

export const getNoteName = (dstPath: string, note: any): string => {
let noteName;

Expand Down
35 changes: 32 additions & 3 deletions src/utils/folder-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,48 @@ import * as path from 'path';
import { Path } from '../paths';
import { yarleOptions } from '../yarle';

import { getNoteFileName, getNoteName } from './filename-utils';
import { getNoteFileName, getNoteName, getUniqueId, normalizeTitle } from './filename-utils';
import { loggerInfo } from './loggerInfo';
import { logger } from './logger';
import { OutputFormat } from './../output-format';
import { RuntimePropertiesSingleton } from './../runtime-properties';

export const paths: Path = {};
const MAX_PATH = 249;

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

export const truncatFileName = (fileName: string, uniqueId: string): string => {

if (fileName.length <= 11) {
throw Error('FATAL: note folder directory path exceeds the OS limitation. Please pick a destination closer to the root folder.');
}

const fullPath = `${getNotesPath()}${path.sep}${fileName}`;

return fullPath.length <  MAX_PATH ? fileName : `${fileName.slice(0, MAX_PATH - 11)}_${uniqueId}.md`;
};

const truncateFilePath = (note: any, fileName: string, fullFilePath: string): string => {
const noteIdNameMap = RuntimePropertiesSingleton.getInstance();

const noteIdMap = noteIdNameMap.getNoteIdNameMapByNoteTitle(normalizeTitle(note.title))[0] || {uniqueEnd: getUniqueId()};


if (fileName.length <= 11) {
throw Error('FATAL: note folder directory path exceeds the OS limitation. Please pick a destination closer to the root folder.');
}

return `${fullFilePath.slice(0, MAX_PATH - 11)}_${noteIdMap.uniqueEnd}.md`;
// -11 is the nanoid 5 char +_+ the max possible extension of the note (.md vs .html)
};

const getFilePath = (dstPath: string, note: any): string => {
return `${dstPath}${path.sep}${getNoteFileName(dstPath, note)}`;
const fileName = getNoteFileName(dstPath, note);
const fullFilePath = `${dstPath}${path.sep}${normalizeTitle(fileName)}`;

return fullFilePath.length <  MAX_PATH ? fullFilePath : truncateFilePath(note, fileName, fullFilePath);
};

export const getMdFilePath = (note: any): string => {
Expand Down
8 changes: 4 additions & 4 deletions src/utils/turndown-rules/internal-links-rule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import marked, { Token } from 'marked';
import * as _ from 'lodash';

import { normalizeTitle } from '../filename-utils';
import { getUniqueId, normalizeTitle } from '../filename-utils';
import { OutputFormat } from '../../output-format';
import { yarleOptions } from '../../yarle';
import { getTurndownService } from '../turndown-service';
Expand All @@ -25,7 +25,6 @@ export const wikiStyleLinksRule = {
if (!nodeProxy.href) {
return '';
}

let internalTurndownedContent =
getTurndownService(yarleOptions).turndown(removeBrackets(node.innerHTML));
internalTurndownedContent = removeDoubleBackSlashes(internalTurndownedContent);
Expand Down Expand Up @@ -64,10 +63,11 @@ export const wikiStyleLinksRule = {
if (value.startsWith('evernote://')) {
const fileName = normalizeTitle(token['text']);
const noteIdNameMap = RuntimePropertiesSingleton.getInstance();
const uniqueId = getUniqueId();
if (isTOC(noteIdNameMap.getCurrentNoteName())) {
noteIdNameMap.addItemToTOCMap({ url: value, title: fileName });
noteIdNameMap.addItemToTOCMap({ url: value, title: fileName, uniqueEnd: uniqueId });
} else {
noteIdNameMap.addItemToMap({ url: value, title: fileName });
noteIdNameMap.addItemToMap({ url: value, title: fileName, uniqueEnd: uniqueId });
}

const linkedNoteId = value;
Expand Down
9 changes: 9 additions & 0 deletions test/data/test-long-linked-notes-NoteB.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# NoteB



This is the content of NoteB, and a reference to [[This is going to be a really reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreall_7Kcx7.md|This is going to be a really reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreally note title]]

Created at: 2020-05-28T15:14:04+01:00
Updated at: 2020-05-28T15:14:42+01:00
Notebook: test-long-linked-notes
6 changes: 6 additions & 0 deletions test/data/test-long-linked-notes.enex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export3.dtd">
<en-export export-date="20200528T141459Z" application="Evernote" version="Evernote Mac 7.14 (458265)">
<note><title>NoteB</title><content><![CDATA[<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div>This is the content of NoteB, and a reference to <a href="evernote:///view/16093948/s150/acbca7f9-5e63-48d8-9d20-698facc936e8/acbca7f9-5e63-48d8-9d20-698facc936e8/" style="color: rgb(105, 170, 53);">This is going to be a really reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreally note title</a></div></en-note>]]></content><created>20200528T141404Z</created><updated>20200528T141442Z</updated><note-attributes><latitude>46.37570190429688</latitude><longitude>18.13928805659798</longitude><altitude>128.9876251220703</altitude><author>akos</author><source>desktop.mac</source><reminder-order>0</reminder-order></note-attributes></note>
<note><title>This is going to be a really reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreally note title</title><content><![CDATA[<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div>This is the content of NoteA</div></en-note>]]></content><created>20200528T141349Z</created><updated>20200528T141402Z</updated><note-attributes><latitude>46.37570190429688</latitude><longitude>18.13928805659798</longitude><altitude>128.9876251220703</altitude><author>akos</author><source>desktop.mac</source><reminder-order>0</reminder-order></note-attributes></note>
</en-export>
5 changes: 5 additions & 0 deletions test/data/test-long-note.enex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export3.dtd">
<en-export export-date="20181006T101455Z" application="Evernote" version="Evernote Mac 7.5 (457109)">
<note><title>This is going to be a really reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreally note title</title><content><![CDATA[<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div>text1</div><div><br /></div></en-note>]]></content><created>19691006T084413Z</created><updated>20181006T101436Z</updated><note-attributes><author>akos</author><source>desktop.mac</source><reminder-order>0</reminder-order></note-attributes></note>
</en-export>
Loading

0 comments on commit 9510fac

Please sign in to comment.