import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=469)
winform.add(
button={cls="button";text="读取";left=669;top=16;right=748;bottom=43;dr=1;dt=1;z=2};
edit={cls="edit";text="https://www.chinadaily.com.cn/a/202409/19/WS66ebef8fa3103711928a8b54.html";left=10;top=18;right=656;bottom=44;aw=1;dl=1;dr=1;dt=1;edge=1;multiline=1;z=3};
richedit={cls="richedit";left=9;top=51;right=749;bottom=459;ah=1;aw=1;db=1;dl=1;dr=1;dt=1;edge=1;link=1;multiline=1;vscroll=1;wrap=1;z=1}
)
/*}}*/
html2Markdown=function(html){
import web.turndown;
var turndownService = web.turndown( codeBlockStyle = "fenced" );
turndownService.remove('script');
turndownService.remove('style');
//启用 gfm(GitHub Flavored Markdown)插件。
turndownService.useGfm()
// 添加自定义规则处理带类名的代码块
turndownService.addRule('codeBlock', "{
filter: function(node) {
return node.nodeName === 'PRE' && node.classList.contains('code');
},
replacement: function(content, node) {
var language = node.classList.item(1) || '';
language = language.replace(/^language-/, '');
return '```' + language + '\n' + content.trim() + '\n```';
}
}");
return turndownService.turndown(html);
}
winform.button.oncommand = function(id,event){
import inet.http
var http=inet.http()
var html=http.get(winform.edit.text)
var contentStr=string.match(html,`\<div id="Content"\>(.*?)\</div\>`)
var content=html2Markdown(contentStr)
winform.richedit.text=content
}
winform.show();
win.loopMessage();
群里有人在问获取网页内容,就顺手写了一个,代码基本上用的范例中的,web应用-HTML-HTML转Markdown。