Skip to content

Commit

Permalink
TODO: 嵌套标签解析问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ipcjs committed Jul 5, 2024
1 parent 5e6610a commit b1fcca3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
29 changes: 18 additions & 11 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@
"program": "${workspaceFolder}/dist/dropTheRope.js",
"args": [
"--configFile", "./config.obsidian.json",
// "--outputDir", "C:\\Users\\ipcjs\\Dropbox\\Notes",
// "--outputDir", "C:\\Users\\ipcjs\\Dropbox\\Notes", "--outputMarkdownDirName", "Evernote", "--enexSources", "K:\\Backup\\Evernote",
// "--outputDir", "/Users/ipcjs/Library/CloudStorage/Dropbox/Notes", "--outputMarkdownDirName", "Evernote", "--enexSources", "/Volumes/Data/data/Evernote",
// "--enexSources", "K:\\Backup\\Evernote\\Coding\\Coding@@@Dart.enex",
// "--enexSources", "K:\\Backup\\Evernote\\Coding\\Coding@@@JavaScript.enex",
// "--enexSources", "K:\\Backup\\Evernote\\Others\\Others@@@移动设备笔记.enex",
// "--enexSources", "C:\\Users\\ipcjs\\Dropbox\\Notes\\yarle-test",
// "--enexSources", "C:\\Users\\ipcjs\\Dropbox\\Notes\\yarle-test\\测试链接和转义.enex",
// "--enexSources", "C:\\Users\\ipcjs\\Dropbox\\Notes\\yarle-test\\测试转义.enex",
// "--enexSources", "C:\\Users\\ipcjs\\Dropbox\\Notes\\yarle-test\\测试下划线.enex",
// "--enexSources", "C:\\Users\\ipcjs\\Dropbox\\Notes\\yarle-test\\测试带链接的图片.enex",
// "--outputDir", "${env:DROPBOX}/Notes",
// "--outputDir", "${env:DROPBOX}/Notes", "--outputMarkdownDirName", "Evernote", "--enexSources", "K:/Backup/Evernote",
// "--outputDir", "${env:DROPBOX}/Notes", "--outputMarkdownDirName", "Evernote", "--enexSources", "/Volumes/Data/data/Evernote",
// "--outputMarkdownDirName", "Evernote-051523", "--enexSources", "K:/Backup/Evernote",
// "--enexSources", "K:/Backup/Evernote/Coding/Coding@@@Dart.enex",
// "--enexSources", "K:/Backup/Evernote/Coding/Coding@@@JavaScript.enex",
// "--enexSources", "K:/Backup/Evernote/Others/Others@@@移动设备笔记.enex",
// "--enexSources", "${env:DROPBOX}/Notes/yarle-test",
// "--enexSources", "${env:DROPBOX}/Notes/yarle-test/测试链接和转义.enex",
// "--enexSources", "${env:DROPBOX}/Notes/yarle-test/测试转义.enex",
// "--enexSources", "${env:DROPBOX}/Notes/yarle-test/测试下划线.enex",
// "--enexSources", "${env:DROPBOX}/Notes/yarle-test/测试带链接的图片.enex",
// "--enexSources", "/Volumes/Data/data/Evernote/Coding/Coding@@@Dart.enex"
// "--enexSources", "${env:DROPBOX}/Notes/yarle-test/测试缩进代码块.enex",
// "--enexSources", "${env:DROPBOX}/Notes/yarle-test/test escape.enex",
// "--enexSources", "${env:DROPBOX}/Notes/yarle-test/test_table.enex",
// "--enexSources", "${env:DROPBOX}/Notes/yarle-test/vim查找替换及正则表达式的使用.enex",
"--enexSources", "${env:DROPBOX}/Notes/yarle-test/test_list.enex",
"--enexSources", "${env:DROPBOX}/Notes/yarle-test/test_list2.enex",
],
"preLaunchTask": "npm: build:watch",
},
Expand Down
2 changes: 2 additions & 0 deletions src/convert-html-to-md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const fixSublistsInContent = (content: string): string => {
};

const fixSublists = (node: HTMLElement) => {
// TODO: 2024/07/04 ipcjs ul>ol>li时ul节点里面的文字被移除的问题
return node
const ulElements: Array<HTMLElement> = Array.from(node.getElementsByTagName('ul'));
const olElements: Array<HTMLElement> = Array.from(node.getElementsByTagName('ol'));
const listElements = ulElements.concat(olElements);
Expand Down
15 changes: 15 additions & 0 deletions src/utils/turndown-rules/list-rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** @see https://github.com/mixmark-io/turndown/blob/master/src/commonmark-rules.js/#L48 */
export const listRule = {
filter: ['ul', 'ol'],
replacement: function (content: string, node: HTMLUListElement | HTMLOListElement) {
var parent = node.parentNode
if (parent.nodeName === 'LI' && parent.lastElementChild === node
|| parent.nodeName === 'UL'
|| parent.nodeName === 'OL'
) {
return '\n' + content
} else {
return '\n\n' + content + '\n\n'
}
}
}
21 changes: 12 additions & 9 deletions src/utils/turndown-rules/task-list-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,27 @@ export const taskListRule = {
filter: 'li',
replacement: (content: string, node: HTMLLIElement, options: any) => {

const isTodoDoneBlock = (node: any) => {
const isTodoDoneBlock = (node: any) => {
const nodeProxy = getAttributeProxy(node);
const taskFlag = '--en-checked:true;';

return nodeProxy.style && nodeProxy.style.value.indexOf(taskFlag) >= 0;
};
const isTodoBlock = (node: any) => {
const isTodoBlock = (node: any) => {
const nodeProxy = getAttributeProxy(node);
const taskFlag = '--en-checked:false;';

return nodeProxy.style && nodeProxy.style.value.indexOf(taskFlag) >= 0;
};

const indentCount = content.match(/^\n*/)[0].length || 0;
const indentCount = content.match(/^\n*/)[0].length || 0;
const indentChars = yarleOptions.indentCharacter.repeat(indentCount);

const todoPrefix = yarleOptions.outputFormat === OutputFormat.LogSeqMD ? '' :
node.parentElement?.nodeName?.toUpperCase() === 'LI' ? '' : `${indentChars}- `;
const todoPrefix = yarleOptions.outputFormat === OutputFormat.LogSeqMD
? ''
: node.parentElement?.nodeName?.toUpperCase() === 'LI'
? ''
: `${indentChars}- `;


const singleLineContent = content
Expand All @@ -34,14 +37,13 @@ export const taskListRule = {
.replace(/\n/gm, `\n${yarleOptions.indentCharacter}`); // Indent
const languageItems = getLanguageItems(yarleOptions.outputFormat);

let prefix = indentCount > 0
let prefix = indentCount > 0
? indentChars
: (isTodoDoneBlock(node)
? `${checkboxDone} `
: (isTodoBlock(node)
? `${checkboxTodo} `
: languageItems.listItem))
;
: languageItems.listItem));
const parent = node.parentNode;
if (parent.nodeName === 'OL') {
const start = (parent as HTMLOListElement).getAttribute('start');
Expand All @@ -54,4 +56,5 @@ export const taskListRule = {
ret = (prefix + singleLineContent + (node.nextSibling && !/\n$/.test(singleLineContent) ? '\n' : ''));

return ret;
}};
}
};
4 changes: 3 additions & 1 deletion src/utils/turndown-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { removeNewlines } from './remove-newlines';
import { tanaTableBlock, tanaTableColBlock, tanaTableRowBlock } from './../constants';
import { underlineRule } from './turndown-rules/underline-rule';
import { encryptRule } from './turndown-rules/encrypt-rule';
import { listRule } from './turndown-rules/list-rule';

export const getTurndownService = (yarleOptions: YarleOptions) => {
/* istanbul ignore next */
Expand All @@ -39,7 +40,8 @@ export const getTurndownService = (yarleOptions: YarleOptions) => {
turndownService.addRule('evernote task items', taskItemsRule);
turndownService.addRule('wikistyle links', wikiStyleLinksRule);
turndownService.addRule('images', imagesRule);
turndownService.addRule('list', taskListRule);
turndownService.addRule('list', listRule);
// turndownService.addRule('task-list', taskListRule);
turndownService.addRule('italic', italicRule);
turndownService.addRule('underline', underlineRule);
turndownService.addRule('encrypt', encryptRule);
Expand Down

0 comments on commit b1fcca3

Please sign in to comment.