DasRed / js-bbcode-parser

24 stars 7 forks source link

editor for js-bbcode-parser #9

Closed AndreiSoroka closed 3 years ago

AndreiSoroka commented 3 years ago

Hi. Is exist any editor for js-bbcode-parser?

DasRed commented 3 years ago

Hey,

great to see you, but this is only a parser. it is not designed for any editor. This parser get the complete text from any source (eg. an editor) and will parse the BBCode and transform them to HTML.

best DR

AndreiSoroka commented 3 years ago

Yep. I see)

I mean... Maybe any editor uses js-bbcode-parser as the core for parse...

Ok. Thanks)

AndreiSoroka commented 3 years ago

Ok. I take vue2editor (you can take other) and example html to bbcode as base

After that take bbcode to html function and revert as the example html to bbcode

export default function convertedHtmlToBBCode(html) {
  let convertedHtml = html;
  // br
  convertedHtml = convertedHtml.replace(/<br(.*?)>/gi, '\n');
  // b
  convertedHtml = convertedHtml.replace(/<strong>/gi, '[b]');
  convertedHtml = convertedHtml.replace(/<\/strong>/gi, '[/b]');
  convertedHtml = convertedHtml.replace(/<b>/gi, '[b]');
  convertedHtml = convertedHtml.replace(/<\/b>/gi, '[/b]');
  // i
  convertedHtml = convertedHtml.replace(/<em>/gi, '[i]');
  convertedHtml = convertedHtml.replace(/<\/em>/gi, '[/i]');
  convertedHtml = convertedHtml.replace(/<i>/gi, '[i]');
  convertedHtml = convertedHtml.replace(/<\/i>/gi, '[/i]');
  convertedHtml = convertedHtml.replace(/<cite>/gi, '[i]');
  convertedHtml = convertedHtml.replace(/<\/cite>/gi, '[/i]');
  // u
  convertedHtml = convertedHtml.replace(/<u>/gi, '[u]');
  convertedHtml = convertedHtml.replace(/<\/u>/gi, '[/u]');
  // h
  convertedHtml = convertedHtml.replace(/<h([1-7]).*?>(.*?)<\/h[1-7]>/, '\n[h$1]$2[/h$1]\n');
  // p
  convertedHtml = convertedHtml.replace(/<p.*?>/gi, '[p]');
  convertedHtml = convertedHtml.replace(/<\/p>/gi, '[/p]');
  // color
  convertedHtml = convertedHtml.replace(/<font color="(.*?)">(.*?)<\/font>/gmi, '[color=$1]$2[/color]');
  convertedHtml = convertedHtml.replace(/<font color=(.*?)>(.*?)<\/font>/gmi, '[color=$1]$2[/color]');
  convertedHtml = convertedHtml.replace(/<span style="color:(.*?)">(.*?)<\/span>/gmi, '[color=$1]$2[/color]');
  // link
  convertedHtml = convertedHtml.replace(/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi, '[url=$2]$4[/url]');
  // images
  convertedHtml = convertedHtml.replace(/<img(.*?)src="(.*?)"(.*?)>/gi, '[img]$2[/img]');
  // list
  convertedHtml = convertedHtml.replace(/<li(.*?)>(.*?)<\/li>/gi, '[*]$2[/*]');
  convertedHtml = convertedHtml.replace(/<ul(.*?)>/gi, '[list]');
  convertedHtml = convertedHtml.replace(/<\/ul>/gi, '[/list]');

  // clear
  convertedHtml = convertedHtml.replace(/<link(.*?)>/gi, '');
  convertedHtml = convertedHtml.replace(/<div>/gi, '\n');
  convertedHtml = convertedHtml.replace(/<\/div>/gi, '\n');
  convertedHtml = convertedHtml.replace(/<td(.*?)>/gi, ' ');
  convertedHtml = convertedHtml.replace(/<tr(.*?)>/gi, '\n');
  convertedHtml = convertedHtml.replace(/<head>(.*?)<\/head>/gmi, '');
  convertedHtml = convertedHtml.replace(/<object>(.*?)<\/object>/gmi, '');
  convertedHtml = convertedHtml.replace(/<script(.*?)>(.*?)<\/script>/gmi, '');
  convertedHtml = convertedHtml.replace(/<style(.*?)>(.*?)<\/style>/gmi, '');
  convertedHtml = convertedHtml.replace(/<title>(.*?)<\/title>/gmi, '');
  convertedHtml = convertedHtml.replace(/<!--(.*?)-->/gmi, '\n');
  convertedHtml = convertedHtml.replace(/\/\//gi, '/');
  convertedHtml = convertedHtml.replace(/\r\r/gi, '');
  convertedHtml = convertedHtml.replace(/(\S)\n/gi, '$1 ');
  return convertedHtml;
}