简体   繁体   English

如何在PageDown.js中将rel =“nofollow”属性添加到外部链接?

[英]How to add rel=“nofollow” attribute to external links in PageDown.js?

I'm using PageDown.js to make raw HTML in markdown style like StackOverflow does. 我正在使用PageDown.js来制作像StackOverflow那样的markdown风格的原始HTML。 My code looks like the following in nodejs: 我的代码在nodejs中如下所示:

var saneConv require('./pagedown/Markdown.Sanitizer'),
    resultHTML = saneConv.makeHtml('[raw html string]');

Now I need to add rel="nofollow" to all external links. 现在我需要将rel="nofollow"添加到所有外部链接。 Unfortunately I can't see that ability in PageDown itself. 不幸的是,我无法在PageDown中看到这种能力。

Could you advice me please a solution to add ref="nofollow" attribute to all external links? 你可以建议我为所有外部链接添加ref="nofollow"属性的解决方案吗?

If all links are external then you could simply use 如果所有链接都在外部,那么您可以简单地使用

resultHTML = resultHTML.replace('<a ', '<a rel="nofollow" ');

or there's a function called writeAnchorTag in Markdown.Converter.js that you can change to do what you need. 或者在Markdown.Converter.js中有一个名为writeAnchorTag的函数,您可以将其更改为执行所需操作。

Update 更新

var jsdom = require("jsdom");
var fs = require("fs");
var jquery = fs.readFileSync("./path/to/jquery.js", "utf-8");

var saneConv = require('./pagedown/Markdown.Sanitizer'),
    resultHTML = saneConv.makeHtml('[raw html string]');

jsdom.env(resultHTML, [jquery], function(err, window) {
    var $ = window.$;

    $("a").each(function() {
        var $a = $(this);
        var href = $a.attr('href');

        // figure out if href is external or internal
        // let's say if href doesn't contain specific domain it's external so add rel=nofollow
        if (href.indexOf('example.com') < 0 || href.indexOf('example2.com') < 0) {
            // it's external, let's add rel=nofollow
            $a.attr('rel', 'nofollow');
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 rel=nofollow 添加到 wordpress 中的某些特定外部链接 - how to add rel=nofollow to some specific external links in wordpress 使用 vanilla JS 将 rel=“noopener noreferrer nofollow” 添加到所有外部链接的最佳方法 - Best way to add rel=“noopener noreferrer nofollow” to all external links with vanilla JS 如何使用javascript将rel =“ nofollow”添加到与我的域不相关的所有外部链接? - How to add rel=“nofollow” to all external links that do not related to my domain with javascript? window.open()添加一个rel =“ nofollow”属性 - window.open() add a rel=“nofollow” attribute 如果它是外部链接,如何在CKEditor中的链接中添加rel =“nofollow” - How can I add rel = “nofollow” to a link in CKEditor if it's an external link 将rel =“ nofollow”添加到Facebook评论 - Add rel=“nofollow” to facebook comments 我如何在ckeditor内容的标签上添加rel =“ nofollow” - How i add rel=“nofollow” on a tag in the ckeditor content Stackoverflow WYSIWYG Pagedown.js编辑器将实时预览限制为单击而不是键入 - Stackoverflow WYSIWYG Pagedown.js Editor restricting live preview to on click rather than on keyup JavaScript在返回的数据上添加rel =“ nofollow” - JavaScript Add rel=“nofollow” on Returned Data 如何将“ rel”属性添加到select元素的href? - How to add “rel” attribute to a href of a select element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM