Skip to content

Commit

Permalink
Merge branch 'master' of github.com:akosbalasko/yarle
Browse files Browse the repository at this point in the history
  • Loading branch information
akosbalasko committed Jan 15, 2022
2 parents 7bd3661 + a763e4f commit f1afe20
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 42 deletions.
43 changes: 2 additions & 41 deletions src/dropTheRopeRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as yarle from './yarle';
import { YarleOptions } from './YarleOptions';
import { loggerInfo } from './utils/loggerInfo';
import { clearLogFile } from './utils/clearLogFile';
import { RuntimePropertiesSingleton } from './runtime-properties';
import { applyLinks } from './utils/apply-links';

export const run = async (opts?: YarleOptions) => {
clearLogFile();
Expand All @@ -35,44 +35,5 @@ export const run = async (opts?: YarleOptions) => {
const outputNotebookFolders = await yarle.dropTheRope(options);

// apply internal links
const linkNameMap = RuntimePropertiesSingleton.getInstance();
const allLinks = linkNameMap.getAllNoteIdNameMap();
for (const [linkName, linkProps] of Object.entries(allLinks)) {
const fileName: string = (linkProps as any)['title'];
const notebookName: string = (linkProps as any)['notebookName'];
const encodedFileName = options.urlEncodeFileNamesAndLinks ? encodeURI(fileName as string) : fileName as string;

for (const notebookFolder of outputNotebookFolders) {
let realFileName = encodedFileName;
let realFileNameInContent = encodedFileName;
if (notebookName && !notebookFolder.endsWith(notebookName)) {
realFileName = `${notebookName}${encodedFileName}`;
realFileNameInContent = `${notebookName}/${encodedFileName}`;
}
const filesInOutputDir = fs.readdirSync(notebookFolder);
console.log(`Files in output dir: ${JSON.stringify(filesInOutputDir)}`);
console.log(`notebookFolder: ${notebookFolder}`);
console.log(`realFileName: ${realFileName}`);

const extension = '.md';

const targetFiles = filesInOutputDir.filter(file => {
return path.extname(file).toLowerCase() === extension;
});
for (const targetFile of targetFiles) {
const fileContent = fs.readFileSync(`${notebookFolder}${path.sep}${targetFile}`, 'UTF-8');
const escapedLinkName = escapeEntity(linkName);
const regexp = new RegExp(escapedLinkName, 'g');
const updatedContent = fileContent.replace(regexp, realFileNameInContent);
if (fileContent !== updatedContent) {
console.log(`replaced output written to: ${notebookFolder}${path.sep}${targetFile}`);
fs.writeFileSync(`${notebookFolder}${path.sep}${targetFile}`, updatedContent);
}
}
}
}
};

const escapeEntity = (entity: string): string => {
return entity.replace(/\//g, '\\/');
applyLinks(options, outputNotebookFolders);
};
5 changes: 4 additions & 1 deletion src/ui/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import url from 'url';
import path from 'path';
import electron from 'electron';

import { applyLinks } from '../utils/apply-links';
import * as yarle from '../yarle';
import { loggerInfo } from '../utils/loggerInfo';

Expand Down Expand Up @@ -177,7 +178,9 @@ electron.ipcMain.on('configurationUpdated', (event: any, data: any) => {

electron.ipcMain.on('startConversion', async (event: any, data: any) => {
const settings = mapSettingsToYarleOptions();
await yarle.dropTheRope(settings);
const outputNotebookFolders = await yarle.dropTheRope(settings);
// apply internal links
applyLinks(settings, outputNotebookFolders);

});

Expand Down
50 changes: 50 additions & 0 deletions src/utils/apply-links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* istanbul ignore file */
// tslint:disable:no-console
import * as fs from 'fs';
import * as path from 'path';

import { YarleOptions } from './../YarleOptions';
import { RuntimePropertiesSingleton } from './../runtime-properties';

export const applyLinks = (options: YarleOptions, outputNotebookFolders: Array<string>): void => {
const linkNameMap = RuntimePropertiesSingleton.getInstance();
const allLinks = linkNameMap.getAllNoteIdNameMap();
for (const [linkName, linkProps] of Object.entries(allLinks)) {
const fileName: string = (linkProps as any)['title'];
const notebookName: string = (linkProps as any)['notebookName'];
const encodedFileName = options.urlEncodeFileNamesAndLinks ? encodeURI(fileName as string) : fileName as string;

for (const notebookFolder of outputNotebookFolders) {
let realFileName = encodedFileName;
let realFileNameInContent = encodedFileName;
if (notebookName && !notebookFolder.endsWith(notebookName)) {
realFileName = `${notebookName}${encodedFileName}`;
realFileNameInContent = `${notebookName}/${encodedFileName}`;
}
const filesInOutputDir = fs.readdirSync(notebookFolder);
console.log(`Files in output dir: ${JSON.stringify(filesInOutputDir)}`);
console.log(`notebookFolder: ${notebookFolder}`);
console.log(`realFileName: ${realFileName}`);

const extension = '.md';

const targetFiles = filesInOutputDir.filter(file => {
return path.extname(file).toLowerCase() === extension;
});
for (const targetFile of targetFiles) {
const fileContent = fs.readFileSync(`${notebookFolder}${path.sep}${targetFile}`, 'UTF-8');
const escapedLinkName = escapeEntity(linkName);
const regexp = new RegExp(escapedLinkName, 'g');
const updatedContent = fileContent.replace(regexp, realFileNameInContent);
if (fileContent !== updatedContent) {
console.log(`replaced output written to: ${notebookFolder}${path.sep}${targetFile}`);
fs.writeFileSync(`${notebookFolder}${path.sep}${targetFile}`, updatedContent);
}
}
}
}
};

const escapeEntity = (entity: string): string => {
return entity.replace(/\//g, '\\/');
};

0 comments on commit f1afe20

Please sign in to comment.