Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tana highlights fixed #474

Merged
merged 6 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.tana.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"skipWebClips": false,
"skipTags": false,
"useHashTags": false,
"outputFormat": "ObsidianMD",
"outputFormat": "Tana Internal Format",
"taskOutputFormat": "ObsidianMD",
"keepImageSize": "ObsidianMD",
"skipEnexFileNameFromOutputPath": false,
Expand Down
5 changes: 4 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

export const tanaCodeBlock ='<YARLE_TANA_CODE_BLOCK>';
export const checkboxTodo = '- [ ]'
export const checkboxDone = '- [x]'
export const checkboxDone = '- [x]'
export const tanaTableBlock = '<YARLE_TANA_TABLE>';
export const tanaTableRowBlock = '<YARLE_TANA_TABLE_ROW>'
export const tanaTableColBlock = '<YARLE_TANA_TABLE_COL>'
3 changes: 3 additions & 0 deletions src/utils/remove-newlines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const removeNewlines = (content: string): string => {
return content.replace(/\r|\n/g,'')
}
64 changes: 38 additions & 26 deletions src/utils/tana/convert-to-tana-node.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,64 @@
import { NoteData } from "../../models"
import { NodeType, TanaIntermediateFile, TanaIntermediateNode } from "./types"
import { RuntimePropertiesSingleton } from '../../runtime-properties';
import { checkboxDone, checkboxTodo, tanaCodeBlock } from "../../constants";
import { checkboxDone, checkboxTodo, tanaCodeBlock, tanaTableBlock, tanaTableColBlock, tanaTableRowBlock } from "../../constants";
import { createNewTanaFile } from "./create-new-tana-file";

export const cleanTanaContent = (content: string, valueToClean: string):string => {
return content.replace(valueToClean, '')
}
const convertString2TanaNode = (content: string, data: NoteData, addTags: boolean = false): TanaIntermediateNode => {
const linkNameMap = RuntimePropertiesSingleton.getInstance();
const link = linkNameMap.getNoteIdNameMapByNoteTitle(content);
const uid = link && link[0] ? link[0].uniqueEnd : 'uuid' + Math.random()

const tanaNode: TanaIntermediateNode = {
const createTanaNode = (type: NodeType, content: string, data: NoteData, uid: string = 'uuid' + Math.random()): TanaIntermediateNode => {
return {
uid,
createdAt: new Date(data.createdAt).getTime(),
editedAt: new Date(data.updatedAt).getTime(),
type: 'node' as NodeType,
type,
name: content,
refs: [],
children: []
}
}
const convertString2TanaNode = (content: string, data: NoteData, addTags: boolean = false): TanaIntermediateNode => {
const linkNameMap = RuntimePropertiesSingleton.getInstance();
const link = linkNameMap.getNoteIdNameMapByNoteTitle(content);
const uid = link && link[0] ? link[0].uniqueEnd : 'uuid' + Math.random()

const tanaNode: TanaIntermediateNode = createTanaNode('node' as NodeType, content, data, uid);
if (addTags){
const tags = JSON.parse(data.tags)
tanaNode.supertags = tags
}
return tanaNode
}
const convertString2TanaCheckbox = (content: string, todoState: string, data: NoteData): TanaIntermediateNode => {
return {
uid: 'uuid' + Math.random(),
createdAt: new Date(data.createdAt).getTime(),
editedAt: new Date(data.updatedAt).getTime(),
type: 'node' as NodeType,
name: cleanTanaContent(content, todoState === 'todo' ? checkboxTodo: checkboxDone),
todoState: todoState as "todo"|"done",
refs:[],
children: []
}
const checkboxNode = createTanaNode(
'node' as NodeType,
cleanTanaContent(content, todoState === 'todo' ? checkboxTodo: checkboxDone),
data)
checkboxNode.todoState = todoState as "todo"|"done"
return checkboxNode;

}
const convertString2TanaCodeblock = (content: string, data: NoteData): TanaIntermediateNode => {
return {
uid: 'uuid' + Math.random(),
createdAt: new Date(data.createdAt).getTime(),
editedAt: new Date(data.updatedAt).getTime(),
type: 'codeblock' as NodeType,
name: cleanTanaContent(content, tanaCodeBlock),
refs:[],
children: []
return createTanaNode('codeblock' as NodeType, cleanTanaContent(content, tanaCodeBlock), data);

}
const convertString2TanaTable = (content: string, data: NoteData): TanaIntermediateNode => {
const mainTableNode: TanaIntermediateNode = createTanaNode('node' as NodeType, 'Table', data);

const rows = content.split(tanaTableRowBlock)
for (const row of rows){
const cols = row.split(tanaTableColBlock)
const rowNode: TanaIntermediateNode = createTanaNode('node' as NodeType, row, data);

for (const col of cols) {
const colNode: TanaIntermediateNode =createTanaNode('field' as NodeType, col, data);
rowNode.children.push(colNode)
}
mainTableNode.children.push(rowNode)

}
return mainTableNode
}
export const convertChild = (data: NoteData, child: string) => {
// if it is a link
Expand All @@ -63,6 +73,8 @@ export const convertChild = (data: NoteData, child: string) => {
convertedChild = convertString2TanaCheckbox(child, 'todo', data)
if (child.startsWith(checkboxDone))
convertedChild = convertString2TanaCheckbox(child, 'done', data)
if (child.startsWith(tanaTableBlock))
convertedChild = convertString2TanaTable(child.replace(new RegExp(tanaTableBlock, 'g'), ''), data)
if (found && found.length > 0){
for (const link of found){
const pureLink = link.slice(0, -1)
Expand Down
5 changes: 3 additions & 2 deletions src/utils/turndown-rules/italic-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export const italicRule = {
filter: ['i'],
replacement: (content: any) => {
const languageItems = getLanguageItems(yarleOptions.outputFormat);

return `${languageItems.italic}${content}${languageItems.italic}`;
return (content.trim() !== '')
? `${languageItems.italic}${content}${languageItems.italic}`
: content
},
};
2 changes: 1 addition & 1 deletion src/utils/turndown-rules/span-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const spanRule = {
const languageItems = getLanguageItems(yarleOptions.outputFormat);
//const HIGHLIGHT_SEPARATOR = yarleOptions.outputFormat === OutputFormat.ObsidianMD ? '==' : '`' ;
const nodeProxy = getAttributeProxy(node);
if (nodeProxy.style) {
if (nodeProxy.style && content.trim() !== '') {
const nodeValue: string = nodeProxy.style.value;

// this aims to care for bold text generated as <span style="font-weight: bold;">Bold</span>
Expand Down
22 changes: 22 additions & 0 deletions src/utils/turndown-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
wikiStyleLinksRule } from './turndown-rules';
import { OutputFormat } from './../output-format';
import { taskListRule } from './turndown-rules/task-list-rule';
import { removeNewlines } from './remove-newlines';
import { tanaTableBlock, tanaTableColBlock, tanaTableRowBlock } from './../constants';

export const getTurndownService = (yarleOptions: YarleOptions) => {
/* istanbul ignore next */
Expand Down Expand Up @@ -48,6 +50,26 @@ export const getTurndownService = (yarleOptions: YarleOptions) => {
});
}

if(yarleOptions.outputFormat === OutputFormat.Tana) {
turndownService.addRule('tanaSkipTableRule', {
filter: ['table'],
replacement(content: any) {
return `${tanaTableBlock}${removeNewlines(content)}`;
},
})
turndownService.addRule('tanaSkipTableRowRule', {
filter: ['tr'],
replacement(content: any) {
return `${tanaTableRowBlock}${removeNewlines(content)}`;
},
})
turndownService.addRule('tanaSkipTableColRule', {
filter: ['td'],
replacement(content: any) {
return `${tanaTableColBlock}${removeNewlines(content)}`;
},
})
}
if (yarleOptions.keepMDCharactersOfENNotes) {
turndownService.escape = ((str: string) => str);
}
Expand Down
2,865 changes: 2,865 additions & 0 deletions test/data/tana-highlight-test-2.enex

Large diffs are not rendered by default.

Loading