Замена javascript шорткодов для ссылки - PullRequest
0 голосов
/ 28 октября 2019

У меня есть веб-страница с текстом и шорткодами. Я хотел бы заменить шорткоды на ссылки:

[a href="http://www.myurl.com" title="A Link Title"]My link text[/a] and even here [a href="www.yoururl.com" title="Another link" class="linkClass"]This is cool to[/a] !

На это

<a href="http://www.myurl.com" title="A Link Title">My link text</a> and even here <a href="www.yoururl.com" title="Another link" class="linkClass">This is cool to</a> !

Не могли бы вы помочь мне сделать это в JQuery или Javascript?

Спасибо заваша помощь.

Ответы [ 2 ]

0 голосов
/ 28 октября 2019

Я наконец нашел, как это сделать с помощью этого сценария: https://github.com/nicinabox/shortcode.js

Вот мой код для обработки моего дела:

new Shortcode(document.querySelector('body'), {
  a: function(done) {
    var ret='<a';
    if (typeof this.options.href !== 'undefined' && this.options.href !== null) ret+=' href="'+this.options.href+'"';
    if (typeof this.options.class !== 'undefined' && this.options.class !== null) ret+=' class="'+this.options.class+'"';
    if (typeof this.options.title !== 'undefined' && this.options.title !== null) ret+=' title="'+this.options.title+'"';
    if (typeof this.options.style !== 'undefined' && this.options.style !== null) ret+=' style="'+this.options.title+'"';
    if (typeof this.options.target !== 'undefined' && this.options.target !== null) ret+=' target="'+this.options.title+'"';
    ret+='>'+this.contents+'</a>';
    return ret;
  }
});
0 голосов
/ 28 октября 2019

Попробуйте использовать заменить . Надеюсь, это поможет.

function sayHello() {
   var str = '[a href="http://www.myurl.com" title="A Link Title"]My link text[/a] and even here [a href="www.yoururl.com" title="Another link" class="linkClass"]This is cool to[/a] !';

   while(str.includes("[")){
      str = str.replace("[", "<");
   }

   while(str.includes("]")){
      str = str.replace("]", ">");
   }

   console.log(str);
}
sayHello();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...