Skip to content

Commit

Permalink
feat: htmlToMarkdown (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzhenping authored Jul 15, 2024
1 parent d8064f1 commit 1a98d15
Show file tree
Hide file tree
Showing 8 changed files with 1,029 additions and 1 deletion.
1 change: 1 addition & 0 deletions conf/lang/en-us.ini
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ print_text = Enable Printing

[doc]
word_to_html = Word to HTML
html_to_markdown = HTML to Markdown
modify_doc = Modify Document
comparison = Comparison
save_merge = Save Merge
Expand Down
1 change: 1 addition & 0 deletions conf/lang/zh-cn.ini
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ print_text = 开启打印

[doc]
word_to_html = Word转笔记
html_to_markdown = HTML转Markdown
modify_doc = 修改文档
comparison = 文档比较
save_merge = 保存合并
Expand Down
10 changes: 10 additions & 0 deletions static/js/cherry_markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ $(function () {
})
}
},
{
noIcon: true,
name: 'Htm转Markdown',
onclick: ()=>{
let converter = new HtmlToMarkdownConverter();
converter.handleFileSelect(function (response) {
window.editor.insertValue(response);
})
}
}
]
});

Expand Down
29 changes: 29 additions & 0 deletions static/js/html-to-markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

class HtmlToMarkdownConverter {

handleFileSelect(callback, accept = '.html,.htm') {
let input = document.createElement('input');
input.type = 'file';
input.accept = accept;
input.onchange = (e)=>{
let file = e.target.files[0];
if (!file) {
return;
}
let reader = new FileReader();
reader.onload = (e)=>{
let text = e.target.result;
let markdown = this.convertToMarkdown(text);
callback(markdown);
};
reader.readAsText(file);
};
input.click();
}


convertToMarkdown(html) {
let turndownService = new TurndownService()
return turndownService.turndown(html)
}
}
9 changes: 8 additions & 1 deletion static/js/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,16 @@ $(function () {
layer.msg(messages);
}
converter.replaceHtmlBase64(response.value).then((html)=>{
insertAndClearToMarkdown(html);
let cm = window.editor.cm;
cm.replaceSelection(html);
});
})
} else if (name === 'htmlToMarkdown') {
let converter = new HtmlToMarkdownConverter();
converter.handleFileSelect(function (response) {
let cm = window.editor.cm;
cm.replaceSelection(response);
})
} else {
var action = window.editor.toolbarHandlers[name];

Expand Down
Loading

0 comments on commit 1a98d15

Please sign in to comment.