问题描述
如何使markdown.js在Delphi中的TWebBrowser中以HTML格式显示一个标记文档?
给定包含Markdown的字符串的内容,如何获取markdown.js将该markdown转换为HTML,并将其显示在TWebBrowser组件中,所有这些都在我的Delphi桌面应用程序中?
更新:
遵循Wouter的例子(谢谢Wouter!),如果我这样做:
procedure TForm2.Button1Click(Sender:TObject);
begin
WebBrowser1.Navigate('file:// C:/junk/markdown/lib/markdown.js');
结束
然后我收到一系列警告。首先,从Windows保护我的javascript。我批准并运行一切,但最后我得到:
确定,这样做:
HTML:
<!DOCTYPE html>
< html>
< head>< style> body {font-family:Arial; font-size:small}< / style>< / head>
< body>
< div id =markdown>< / div>
< input type =hiddenid =hidden/>
< script type =text / javascript>
hiddenEl = document.getElementById('hidden');
markdownEl = document.getElementById('markdown');
< / script>
< script src =lib / markdown.js>< / script>
< / body>
< / html>
Delphi代码:
procedure TForm38.Memo1Change(Sender:TObject);
begin
WebBrowser1.OleObject.Document.GetElementByID('hidden')。setAttribute('value',Memo1.Text);
WebBrowser1.OleObject.Document.ParentWindow.execScript('markdownEl.innerHTML = markdown.toHTML(hiddenEl.value)');
结束
procedure TForm38.FormCreate(Sender:TObject);
begin
WebBrowser1.Navigate('file:// c:/!/markdown.html');
结束
当然这只是一个概念证明。
特别是Delphi代码应该检查文档是否被加载,但是这种代码只会分散注意力。
也许你想知道为什么我首先将TMemo的内容写入隐藏的元素?
这是因为在将JavaScript作为字符串构建时,将带有换行符的文本传递给JavaScript函数是很复杂的。你会得到这样的东西:
window.alert(Hello
World);
更新
在我的例子中,我在html文件的子文件夹 lib
中有markdown.js。
确保< script src =lib / markdown.js>< / script>
确实指向Markdown.js的位置
How do I make markdown.js display a markdown document as HTML in a TWebBrowser in Delphi?
Given the contents of a string containing Markdown, how do I get markdown.js to convert that markdown into HTML, and display that HTML in a TWebBrowser component, all inside my Delphi desktop application?
UPDATE:
Following Wouter's example (thank you, Wouter!), if I simply do this:
procedure TForm2.Button1Click(Sender: TObject);
begin
WebBrowser1.Navigate('file://C:/junk/markdown/lib/markdown.js');
end;
Then I get a series of warnings. First, from Windows "protecting" me from the javascript. I approve and run everything, but finally I get:
OK, this works:
HTML:
<!DOCTYPE html>
<html>
<head><style>body{font-family:Arial;font-size:small}</style></head>
<body>
<div id="markdown"></div>
<input type="hidden" id="hidden" />
<script type="text/javascript">
hiddenEl=document.getElementById('hidden');
markdownEl=document.getElementById('markdown');
</script>
<script src="lib/markdown.js"></script>
</body>
</html>
Delphi code:
procedure TForm38.Memo1Change(Sender: TObject);
begin
WebBrowser1.OleObject.Document.GetElementByID('hidden').setAttribute('value', Memo1.Text);
WebBrowser1.OleObject.Document.ParentWindow.execScript('markdownEl.innerHTML = markdown.toHTML(hiddenEl.value)');
end;
procedure TForm38.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('file://c:/!/markdown.html');
end;
Of course this is just a proof of concept. Especially the Delphi code should check if the document was loaded, but that type of code would only be distracting for this example.
Maybe you're wondering why I first write the contents of the TMemo to a hidden element?That's because it's complicated to pass a text with linebreaks to a JavaScript function when you build the JavaScript as a string. You would get something like this:
window.alert("Hello
World");
updateIn my example, I have markdown.js in a subfolder lib
of the html file. Make sure that <script src="lib/markdown.js"></script>
really points to the location of Markdown.js
这篇关于如何使markdown.js在Delphi中的TWebBrowser中显示一个HTML格式的缩写文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!