gpt4 book ai didi

javascript - 使用 enduro.js 在 Markdown 中按钮

转载 作者:行者123 更新时间:2023-11-28 05:42:03 25 4
gpt4 key购买 nike

我遵循了有关如何在 enduro.js 中启用 markdown 的指南:http://www.endurojs.com/docs/using-markdown

这工作正常,但我想要按钮而不是普通的 anchor 链接。我的 Markdown 看起来像这样:

## Title
Paragraph text

[read more](/linktofullarticle)

我希望阅读更多链接为 <button>而不是<a>

抽象器看起来像这样:

// placeholder abstractor
var abstractor = function () {}

// vendor dependencies
var marked = require('marked')
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: true,
sanitize: true,
smartLists: true,
smartypants: false
})

abstractor.prototype.init = function(context) {
return new Promise(function(resolve, reject) {

// initialize abstractor
resolve()
})
}

abstractor.prototype.abstract = function(context) {
return new Promise(function(resolve, reject) {

context['$markdowned_text_hidden'] = true

// creates the markdowned context
context.markdowned_text = marked(context.text)

// abstract directive
return resolve()

})
}

module.exports = new abstractor()

注意:我还想保留标准链接,因为有时该链接应该是一个按钮,有时应该是一个链接。

最佳答案

我建议您在标准 Markdown 之上添加自定义 Markdown 规则。

例如,获取按钮的 Markdown 可能如下所示

## Title
Paragraph text

..[read more](/linktofullarticle)

然后在抽象器代码中捕获它并用正则表达式替换:

context.markdowned_text = marked(context.text)
context.markdowned_text = context.markdowned_text
.replace(/\.\.\[(.*?)\]\((.*?)\)/g, '<a class="btn" href="$2">$1</a>')

注意:使用<button>在这种情况下可能不起作用,因为您希望用户在单击按钮后重定向。只需使用 <a>class="btn"并将其样式设置为按钮。

关于javascript - 使用 enduro.js 在 Markdown 中按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38845040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com