Skip to content

Commit

Permalink
chore: language refactored (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
akosbalasko authored Feb 17, 2024
1 parent 5104066 commit 3ec7ce2
Show file tree
Hide file tree
Showing 20 changed files with 245 additions and 511 deletions.
16 changes: 4 additions & 12 deletions src/dropTheRopeRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import { YarleOptions } from './YarleOptions';
import { loggerInfo } from './utils/loggerInfo';
import { clearLogFile } from './utils/clearLogFile';
import { applyLinks } from './utils/apply-links';
import { createTanaOutput } from './utils/tana/create-tana-output';
import { isTanaOutput } from './utils/tana/is-tana-output';
const { isHeptaOutput } = require('./utils/heptabase/is-hepta-output');
const { zipFolder } = require('./utils/heptabase/zip-folder');
import { LanguageFactory } from './outputLanguages/LanguageFactory';

export const run = async (opts?: YarleOptions) => {
clearLogFile();
Expand Down Expand Up @@ -42,12 +39,7 @@ export const run = async (opts?: YarleOptions) => {
// apply internal links
applyLinks(options, outputNotebookFolders);

// create tana output if it's required
if (isTanaOutput()){
createTanaOutput(options, outputNotebookFolders)
}
if (isHeptaOutput()){
await zipFolder(options, outputNotebookFolders)
}

const langaugeFactory = new LanguageFactory();
const targetLanguage = langaugeFactory.createLanguage(options.outputFormat)
await targetLanguage.postProcess(options, outputNotebookFolders)
};
22 changes: 22 additions & 0 deletions src/outputLanguages/Heptabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { YarleOptions } from "./../YarleOptions";
import { Language } from "./language";
import { zipFolder } from "./../utils/heptabase/zip-folder";
import { StandardMD } from "./StandardMD";

export class Heptabase extends StandardMD implements Language {
constructor(){
super()
}

languageItems = {
bold: '**',
italic: '_',
highlight: '==',
strikethrough: '~~',
listItem: '* '
};
postProcess = async(options: YarleOptions, outputNotebookFolders: string[]) => {
await zipFolder(options, outputNotebookFolders)
};

}
29 changes: 29 additions & 0 deletions src/outputLanguages/LanguageFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { OutputFormat } from "./../output-format";
import { Language } from "./language";
import { ObsidianMD } from "./ObsidianMD";
import { StandardMD } from "./StandardMD";
import { Heptabase } from "./Heptabase";
import { Tana } from "./Tana";

import { LanguageItems } from "./outputLanguages";

export class LanguageFactory {
createLanguage(type: OutputFormat): Language {
switch (type) {
case OutputFormat.ObsidianMD:
return new ObsidianMD();
case OutputFormat.Heptabase:
return new Heptabase();
case OutputFormat.Tana:
return new Tana();
default:
return new StandardMD();
}
}
}

export const getLanguageItems = (language: OutputFormat): LanguageItems => {
const factory = new LanguageFactory();
const lang = factory.createLanguage(language);
return lang.languageItems;
}
17 changes: 17 additions & 0 deletions src/outputLanguages/ObsidianMD.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { StandardMD } from "./StandardMD";
import { Language } from "./language";

export class ObsidianMD extends StandardMD implements Language {
constructor(){
super()
}

languageItems = {
bold: '**',
italic: '_',
highlight: '==',
strikethrough: '~~',
listItem: '* '
};

}
49 changes: 49 additions & 0 deletions src/outputLanguages/StandardMD.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { cloneDeep } from "lodash";
import { NoteData } from "./../models/NoteData";
import { saveMdFile } from "./../utils";
import { YarleOptions } from "./../YarleOptions";
import { Language } from "./language";

export class StandardMD implements Language {
constructor(){}

languageItems = {
bold: '**',
italic: '_',
highlight: '`',
strikethrough: '~~',
listItem: '* '
};
codeBlock = '\n```\n';

postProcess= async(options: YarleOptions, outputNotebookFolders: string[]) => {};
noteExtension= '.md';
noteProcess= (data: NoteData, note: any) => {
data = (data)

saveMdFile(fixImagesInLink(data.content), note)
};
tagProcess= (content: string, tasks: Map<string, string>, currentTaskPlaceholder: string, updatedContent: string): string => {
return updatedContent;
}

}


const fixImagesInLink = (content: string):string => {
let updatedContent = cloneDeep(content);
// Regular expression for the whole string with two groups
const patternWholeString = /\[!\[\[(.*?)(?:\|(.*?))?\]\]\]\((.*?)\)/g;

let match;
while ((match = patternWholeString.exec(content)) !== null) {
const bracketContent = match[1];
const dimensions = match[2] || ''; // Use empty string if dimensions are not present
const parenthesesContent = match[3];
updatedContent = (dimensions === "")
? updatedContent.replace(`[![[${bracketContent}]]](${parenthesesContent})`, `![${parenthesesContent}](${bracketContent})`)
: updatedContent.replace(`[![[${bracketContent}|${dimensions}]]](${parenthesesContent})`, `![${parenthesesContent}\\|${dimensions}](${bracketContent})`)

}
return updatedContent;
}
61 changes: 61 additions & 0 deletions src/outputLanguages/Tana.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { YarleOptions } from "./../YarleOptions";
import { Language } from "./language";
import { createTanaOutput } from "./../utils/tana/create-tana-output";
import { StandardMD } from "./StandardMD";
import { NoteData } from "./../models/NoteData";
import { cleanTanaContent, convert2TanaNode } from "./../utils/tana/convert-to-tana-node";
import { saveTanaFile } from "./../utils/save-tana-file";
import { NodeType } from "./../utils/tana/types";
import { checkboxDone, checkboxTodo } from './../constants';

export class Tana extends StandardMD implements Language {
constructor(){
super()
}

languageItems = {
bold: '**',
italic: '__',
highlight: '^^',
strikethrough: '~~',
listItem: ''
};
codeBlock ='<YARLE_TANA_CODE_BLOCK>';

postProcess = async (options: YarleOptions, outputNotebookFolders: string[]) => {
createTanaOutput(options, outputNotebookFolders);
};
noteExtension = '.json';
noteProcess = (data: NoteData, note: any) => {

const tanaJson = convert2TanaNode(data)
saveTanaFile(tanaJson, note)
};
tagProcess = (content: string, tasks: Map<string, string>, currentTaskPlaceholder: string, updatedContent: string): string => {
const tanaNote = JSON.parse(content);
const rootTaskChild = tanaNote.nodes?.[0].children?.find((child:any) => child.name === currentTaskPlaceholder)
if (rootTaskChild){
for (const taskItem of tasks.values()){
// split by tasks
const todoState = taskItem.startsWith(checkboxTodo)? 'todo':'done'
tanaNote.nodes?.[0].children?.push({

uid: 'uuid' + Math.random(),
createdAt: rootTaskChild.createdAt,
editedAt: rootTaskChild.editedAt,
type: 'node' as NodeType,

name: cleanTanaContent(taskItem, todoState === 'todo' ? checkboxTodo: checkboxDone),
todoState: todoState as "todo"|"done",
refs:[],
}

)
}
tanaNote.nodes?.[0].children.splice(tanaNote.nodes?.[0].children.indexOf(rootTaskChild), 1)
return JSON.stringify(tanaNote)
}
return updatedContent
}

}
17 changes: 17 additions & 0 deletions src/outputLanguages/language.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface LanguageItems {

bold?: string;
italic?: string;
highlight?: string;
strikethrough?: string;
listItem?: string;
}

export interface Language {
languageItems: LanguageItems;
postProcess: Function;
noteProcess: Function;
tagProcess: Function;
noteExtension: string;
codeBlock: string;
}
15 changes: 11 additions & 4 deletions src/outputLanguages/outputLanguages.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@

import { OutputFormat } from "./../output-format"
export interface LanguageItems {

bold?: string;
italic?: string;
highlight?: string;
strikethrough?: string;
strikethrough?: string;
listItem?: string;
}
/*
const languageItems: any = {
}
languageItems[OutputFormat.ObsidianMD] = {
bold: '**',
italic: '_',
highlight: '==',
strikethrough: '~~',
strikethrough: '~~',
listItem: '* '
}
languageItems[OutputFormat.Heptabase] = {
bold: '**',
italic: '_',
highlight: '==',
strikethrough: '~~',
strikethrough: '~~',
listItem: '* '
}
languageItems[OutputFormat.Tana] = {
bold: '**',
italic: '__',
highlight: '^^',
strikethrough: '~~',
listItem: ''
}
languageItems[OutputFormat.StandardMD] = {
bold: '**',
italic: '_',
highlight: '`',
strikethrough: '~~',
listItem: '* '
}
export const getLanguageItems = (language: OutputFormat): any => {
return languageItems[language] || languageItems[OutputFormat.StandardMD]
}
}*/
42 changes: 4 additions & 38 deletions src/process-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ import { convertHtml2Md } from './convert-html-to-md';
import { convert2Html } from './convert-to-html';
import { NoteData } from './models/NoteData';
import { loggerInfo } from './utils/loggerInfo';
import { isTOC } from './utils/is-toc';
import { RuntimePropertiesSingleton } from './runtime-properties';
import { OutputFormat } from './output-format';
import { convert2TanaNode } from './utils/tana/convert-to-tana-node';
import { saveTanaFile } from './utils/save-tana-file';
import { isTanaOutput } from './utils/tana/is-tana-output';
import { cloneDeep } from 'lodash';
import { LanguageFactory } from './outputLanguages/LanguageFactory';

export const processNode = (note: any, notebookName: string): void => {

Expand Down Expand Up @@ -57,26 +52,15 @@ export const processNode = (note: any, notebookName: string): void => {
let data = applyTemplate(noteData, yarleOptions);
// tslint:disable-next-line:no-console
// loggerInfo(`data =>\n ${JSON.stringify(data)} \n***`);

if (isTanaOutput()){
const tanaJson = convert2TanaNode({...noteData, content: data}, yarleOptions)
saveTanaFile(tanaJson, note)
}
else {
data = fixImagesInLink(data)
saveMdFile(data, note);
}
const langaugeFactory = new LanguageFactory();
const targetLanguage = langaugeFactory.createLanguage(yarleOptions.outputFormat)
targetLanguage.noteProcess({...noteData, content: data}, note)

if (yarleOptions.keepOriginalHtml) {
convert2Html(noteData);
saveHtmlFile(noteData, note);
}

/* if (isTOC(noteData.title)) {
const noteIdNameMap = RuntimePropertiesSingleton.getInstance();
noteIdNameMap.extendNoteIdNameMap(noteData);
}*/

} catch (e) {
// tslint:disable-next-line:no-console
loggerInfo(`Failed to convert note: ${noteData.title}, ${JSON.stringify(e)}`);
Expand All @@ -88,21 +72,3 @@ export const processNode = (note: any, notebookName: string): void => {
loggerInfo(`Note "${noteData.title}" converted successfully in ${conversionDuration} seconds.`);

};

const fixImagesInLink = (content: string):string => {
let updatedContent = cloneDeep(content);
// Regular expression for the whole string with two groups
const patternWholeString = /\[!\[\[(.*?)(?:\|(.*?))?\]\]\]\((.*?)\)/g;

let match;
while ((match = patternWholeString.exec(content)) !== null) {
const bracketContent = match[1];
const dimensions = match[2] || ''; // Use empty string if dimensions are not present
const parenthesesContent = match[3];
updatedContent = (dimensions === "")
? updatedContent.replace(`[![[${bracketContent}]]](${parenthesesContent})`, `![${parenthesesContent}](${bracketContent})`)
: updatedContent.replace(`[![[${bracketContent}|${dimensions}]]](${parenthesesContent})`, `![${parenthesesContent}\\|${dimensions}](${bracketContent})`)

}
return updatedContent;
}
Loading

0 comments on commit 3ec7ce2

Please sign in to comment.