plugin.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.10.2 (2021-11-17)
  8. */
  9. ;(function () {
  10. 'use strict'
  11. var global$1 = tinymce.util.Tools.resolve('tinymce.PluginManager')
  12. var global = tinymce.util.Tools.resolve('tinymce.util.Tools')
  13. var html2bbcode = function (s) {
  14. s = global.trim(s)
  15. var rep = function (re, str) {
  16. s = s.replace(re, str)
  17. }
  18. rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi, '[url=$1]$2[/url]')
  19. rep(
  20. /<font.*?color=\"(.*?)\".*?class=\"codeStyle\".*?>(.*?)<\/font>/gi,
  21. '[code][color=$1]$2[/color][/code]'
  22. )
  23. rep(
  24. /<font.*?color=\"(.*?)\".*?class=\"quoteStyle\".*?>(.*?)<\/font>/gi,
  25. '[quote][color=$1]$2[/color][/quote]'
  26. )
  27. rep(
  28. /<font.*?class=\"codeStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,
  29. '[code][color=$1]$2[/color][/code]'
  30. )
  31. rep(
  32. /<font.*?class=\"quoteStyle\".*?color=\"(.*?)\".*?>(.*?)<\/font>/gi,
  33. '[quote][color=$1]$2[/color][/quote]'
  34. )
  35. rep(
  36. /<span style=\"color: ?(.*?);\">(.*?)<\/span>/gi,
  37. '[color=$1]$2[/color]'
  38. )
  39. rep(/<font.*?color=\"(.*?)\".*?>(.*?)<\/font>/gi, '[color=$1]$2[/color]')
  40. rep(
  41. /<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi,
  42. '[size=$1]$2[/size]'
  43. )
  44. rep(/<font>(.*?)<\/font>/gi, '$1')
  45. rep(/<img.*?src=\"(.*?)\".*?\/>/gi, '[img]$1[/img]')
  46. rep(/<span class=\"codeStyle\">(.*?)<\/span>/gi, '[code]$1[/code]')
  47. rep(/<span class=\"quoteStyle\">(.*?)<\/span>/gi, '[quote]$1[/quote]')
  48. rep(
  49. /<strong class=\"codeStyle\">(.*?)<\/strong>/gi,
  50. '[code][b]$1[/b][/code]'
  51. )
  52. rep(
  53. /<strong class=\"quoteStyle\">(.*?)<\/strong>/gi,
  54. '[quote][b]$1[/b][/quote]'
  55. )
  56. rep(/<em class=\"codeStyle\">(.*?)<\/em>/gi, '[code][i]$1[/i][/code]')
  57. rep(/<em class=\"quoteStyle\">(.*?)<\/em>/gi, '[quote][i]$1[/i][/quote]')
  58. rep(/<u class=\"codeStyle\">(.*?)<\/u>/gi, '[code][u]$1[/u][/code]')
  59. rep(/<u class=\"quoteStyle\">(.*?)<\/u>/gi, '[quote][u]$1[/u][/quote]')
  60. rep(/<\/(strong|b)>/gi, '[/b]')
  61. rep(/<(strong|b)>/gi, '[b]')
  62. rep(/<\/(em|i)>/gi, '[/i]')
  63. rep(/<(em|i)>/gi, '[i]')
  64. rep(/<\/u>/gi, '[/u]')
  65. rep(
  66. /<span style=\"text-decoration: ?underline;\">(.*?)<\/span>/gi,
  67. '[u]$1[/u]'
  68. )
  69. rep(/<u>/gi, '[u]')
  70. rep(/<blockquote[^>]*>/gi, '[quote]')
  71. rep(/<\/blockquote>/gi, '[/quote]')
  72. rep(/<br \/>/gi, '\n')
  73. rep(/<br\/>/gi, '\n')
  74. rep(/<br>/gi, '\n')
  75. rep(/<p>/gi, '')
  76. rep(/<\/p>/gi, '\n')
  77. rep(/&nbsp;|\u00a0/gi, ' ')
  78. rep(/&quot;/gi, '"')
  79. rep(/&lt;/gi, '<')
  80. rep(/&gt;/gi, '>')
  81. rep(/&amp;/gi, '&')
  82. return s
  83. }
  84. var bbcode2html = function (s) {
  85. s = global.trim(s)
  86. var rep = function (re, str) {
  87. s = s.replace(re, str)
  88. }
  89. rep(/\n/gi, '<br />')
  90. rep(/\[b\]/gi, '<strong>')
  91. rep(/\[\/b\]/gi, '</strong>')
  92. rep(/\[i\]/gi, '<em>')
  93. rep(/\[\/i\]/gi, '</em>')
  94. rep(/\[u\]/gi, '<u>')
  95. rep(/\[\/u\]/gi, '</u>')
  96. rep(/\[url=([^\]]+)\](.*?)\[\/url\]/gi, '<a href="$1">$2</a>')
  97. rep(/\[url\](.*?)\[\/url\]/gi, '<a href="$1">$1</a>')
  98. rep(/\[img\](.*?)\[\/img\]/gi, '<img src="$1" />')
  99. rep(/\[color=(.*?)\](.*?)\[\/color\]/gi, '<font color="$1">$2</font>')
  100. rep(/\[code\](.*?)\[\/code\]/gi, '<span class="codeStyle">$1</span>&nbsp;')
  101. rep(
  102. /\[quote.*?\](.*?)\[\/quote\]/gi,
  103. '<span class="quoteStyle">$1</span>&nbsp;'
  104. )
  105. return s
  106. }
  107. function Plugin() {
  108. global$1.add('bbcode', function (editor) {
  109. editor.on('BeforeSetContent', function (e) {
  110. e.content = bbcode2html(e.content)
  111. })
  112. editor.on('PostProcess', function (e) {
  113. if (e.set) {
  114. e.content = bbcode2html(e.content)
  115. }
  116. if (e.get) {
  117. e.content = html2bbcode(e.content)
  118. }
  119. })
  120. })
  121. }
  122. Plugin()
  123. })()