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

feat: 新增工具htmlToMarkdown #968

Merged
merged 1 commit into from
Jul 15, 2024
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
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
Loading