如何创建showdown.js降价扩展 [英] How to create a showdown.js markdown extension

查看:83
本文介绍了如何创建showdown.js降价扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,我得到工作输出:

Using the following code, I get working output:

<html>
  <head>
    <script type="text/javascript" src="/js/showdown.js"></script>
  </head>
  <body>
    <script type="text/javascript">
      var converter = new Showdown.converter();
      alert(converter.makeHtml('*test* abc'));
    </script>
  </body>
</html>

返回< p>< em> test< / em> abc< / p>

我现在想添加一个扩展名。 github页面建议可以通过以下方式完成:

I would now like to add an extension. The github page suggests this can be done with:

<script src="src/extensions/twitter.js" />
var converter = new Showdown.converter({ extensions: 'twitter' });

但是,将我的代码修改为:

However, modifying my code to:

<html>
  <head>
    <script type="text/javascript" src="/js/showdown.js"></script>
    <script type="text/javascript" src="/js/twitter.js"></script>
  </head>
  <body>
    <script type="text/javascript">
      var converter = new Showdown.converter({ extensions: 'twitter' });
      alert(converter.makeHtml('*test* abc'));
    </script>
  </body>
</html>

产生错误

"Uncaught Extension 'undefined' could not be loaded.  It was either not found or is not a valid extension."

添加以下代码(如过滤器示例

var demo = function(converter) {
  return [
    // Replace escaped @ symbols
    { type: 'lang', function(text) {
      return text.replace(/\\@/g, '@');
    }}
  ];
}

产生错误未捕获的SyntaxError:意外的令牌(

我想创建一个像这样的扩展 https://github.com/rennat/python-markdown-oembed 解释![视频](youtube_link) ,但目前还不清楚如何开始添加这种支持。

I would like to create an extension like this one https://github.com/rennat/python-markdown-oembed to interpret a ![video](youtube_link), but it's unclear how to begin adding this support.

推荐答案

在你的最后一个块中,你在'lang'之后有一个逗号,后面跟着立即使用函数。这是无效的json。

In your last block you have a comma after 'lang', followed immediately with a function. This is not valid json.

编辑

它似乎自述文件不正确。我不得不传递一个字符串'twitter'的数组。

It appears that the readme was incorrect. I had to to pass an array with the string 'twitter'.

var converter = new Showdown.converter({extensions: ['twitter']});
converter.makeHtml('whatever @meandave2020');
// output "<p>whatever <a href="http://twitter.com/meandave2020">@meandave2020</a></p>"

我提交了拉取请求以更新此内容。

I submitted a pull request to update this.

这篇关于如何创建showdown.js降价扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆