Browse Source

Fix bbcode parsing further

ghorsington 5 years ago
parent
commit
027fcc2ed5
1 changed files with 11 additions and 5 deletions
  1. 11 5
      bot/src/bbcode-parser/bbcode-js.ts

+ 11 - 5
bot/src/bbcode-parser/bbcode-js.ts

@@ -56,7 +56,7 @@ function doReplace(content: string, matches: Replacement[], options: BBCodeConfi
 }
 
 function listItemReplace(options: BBCodeConfig, fullMatch: string, tag: string, value: string) {
-    return '<li>' + doReplace(value.trim(), [{ e: '\\[(\\w+)(?:[= ]([^\\]]+))?]((?:.|[\r\n])*?)\\[/\\1]', func: tagReplace }], options) + '</li>';
+    return '<li>' + doReplace(value.trim(), BBCODE_PATTERN, options) + '</li>';
 }
 
 export var extractQuotedText = function (value: string, parts?: string[]) {
@@ -117,6 +117,7 @@ export var parseParams = function (tagName: string, params: string) {
     return paramMap;
 };
 
+const BBCODE_PATTERN = [{ e: '\\[(\\w+)(?:[= ]([^\\]]+))?]((?:.|[\r\n])*?)\\[/\\1]', func: tagReplace }];
 
 function tagReplace(options: BBCodeConfig, fullMatch: string, tag: string, params: string, value: string) {
     let val: string;
@@ -125,6 +126,14 @@ function tagReplace(options: BBCodeConfig, fullMatch: string, tag: string, param
     let inlineValue = paramsObj[tag];
 
     switch (tag) {
+        case 'attach':
+            return '';
+        case 'spoiler':
+            return '';
+        case 'center':
+            return doReplace(value, BBCODE_PATTERN, options);
+        case 'size':
+            return doReplace(value, BBCODE_PATTERN, options);
         case 'quote':
             val = '<div class="' + options.classPrefix + 'quote"';
             for (let i in paramsObj) {
@@ -243,8 +252,6 @@ interface Replacement {
  * @returns rendered html
  */
 export var render = function (content: string, options?: BBCodeConfig) {
-    var matches: Replacement[] = [], tmp;
-
     options = options || {};
 
     if (!options.classPrefix)
@@ -255,6 +262,5 @@ export var render = function (content: string, options?: BBCodeConfig) {
         options.showQuotePrefix = defaults.showQuotePrefix;
 
     // for now, only one rule
-    matches.push({ e: '\\[(\\w+)(?:[= ]([^\\]]+))?]((?:.|[\r\n])*?)\\[/\\1]', func: tagReplace });
-    return doReplace(content, matches, options);
+    return doReplace(content, BBCODE_PATTERN, options);
 };