eai04191 / md2bb-web

https://md2bb.mizle.net/
MIT License
2 stars 0 forks source link

A few missing tags and quirks #2

Open YellowAfterlife opened 1 year ago

YellowAfterlife commented 1 year ago

Firstly, thank you (and tenmihi) for doing this - thought I'd have to write a pile of regex myself.

1. Steam secretly supports two more header sizes and non-secretly supports horizontal rules,

[h1]h1[/h1] [h2]h2[/h2] [h3]h3[/h3] [h4]h4[/h4] [hr][/hr]

image

But md2bb doesn't.

Hypothetically this is a matter of adding

      {
        name: 'header2',
        regex: /(\n|^)(#{2})[ ](.*)/g,
        replacement: '$1[h2]$3[/h2]',
      },
      {
        name: 'header3',
        regex: /(\n|^)(#{3})[ ](.*)/g,
        replacement: '$1[h3]$3[/h3]',
      },
      {
        name: 'hr',
        regex: /(\n|^)(-{3,})/g,
        replacement: '$1[hr][/hr]',
      },

to md2bb.js, but I was not able to verify this due to my limited familiarity with the matter

What I have tried ``` npm run build > md2bb-web@1.0.0 build > parcel build src/index.html √ Built in 430ms. dist\script.2c6b4f0f.js.map 12.75 KB 3ms dist\script.2c6b4f0f.js 6.09 KB 255ms dist\index.html 1.05 KB 2ms dist\style.4383ad0b.css.map 500 B 2ms dist\style.4383ad0b.css 204 B 7ms ``` ``` npm run start > md2bb-web@1.0.0 start > parcel src/index.html Server running at http://localhost:1234 | Building css-loader.js...Browserslist: caniuse-lite is outdated. Please run: npx browserslist@latest --update-db √ Built in 609ms. Browserslist: caniuse-lite is outdated. Please run: npx browserslist@latest --update-db ``` Upon opening the page: ``` Uncaught TypeError: Super expression must either be null or a function, not object _inherits http://localhost:1234/script.221c08a2.js:218 Md2bb http://localhost:1234/script.221c08a2.js:221 parcelRequire<["../node_modules/md2bb/index.js"] http://localhost:1234/script.221c08a2.js:120 ```

2. `code` should probably be ```code```

As the [code] tag is a code block, not inline code. image


I already run the output through a userscript upon pasting into Steam (to turn ![](img.png) into [previewicon=31285016;sizeOriginal,inline;img.png][/previewicon]) so I have simply added these bits in there. No rush.

eai04191 commented 1 year ago

Hi, thank you for the mention. I understand the problem you claim, but as you know, neither md2bb nor md2bb-web is actively maintained. I think it's possible to fork the library and add regular expressions, but I'm not sure if it's worth it. (I remember having trouble building when I tried that before)

Nowadays, I think creating a library that outputs steam bbcode as a markedjs plugin would be highly scalable and performant, but unfortunately I don't have the time or mental space to start doing it right now.

Therefore, we will put this issue on hold for now. Thank you for your interest.

By the way, below I discovered some new ones that do the same thing, although they weren't made by me. If it seems to solve your problem, try using:

A side note about building this project: Projects where yarn.lock exists must use yarn (in this project, [yarn v1](https://classic.yarnpkg.com/lang/en/)) to install dependencies. Probably because you used npm an incorrect package was installed, causing the error. Yes, Node.js is hell. And it's my laziness for not writing a README :)
YellowAfterlife commented 1 year ago

I did also look at MarkdownToBB, which has its ups and downs.

I since wrote some more mini-fixes ```js // missing headers: text = text.replace(/#{2} (.+)/g, "[h2]$1[/h2]"); text = text.replace(/#{3} (.+)/g, "[h3]$1[/h3]"); // inline code: text = text.replace(/\[code](.+?)\[\/code]/g, "`[u]$1[/u]`"); // join consequent quotes: text = text.replace(/\[\/quote]\n\[quote]/g, "\n"); // github flavoured markdown-style tables: text = text.replace(/\|(.+?)\|\n\|(?:\s*-+\s*\|)+((?:\n\|.+\|$)+)/gm, function(_, header, content) { let out = "[table][tr][th]" + header.split("|").map(h => h.trim()).join("[/th][th]") + "[/th][/tr]"; for (let line of content.trim().split("\n")) { let mt = /^\|(.+?)\|$/.exec(line); if (mt) line = mt[1]; else continue; out += "\n[tr][td]" + line.split("|").map(h => h.trim()).join("[/td][td]") + "[/td][/tr]"; } out += "\n[/table]"; return out; }); // join `[/list][list]` (happens for `- item \n second line\n- item 2`) text = text.replace(/\[\/list]((?:\n[ \t]+.*?)+)\n\[list]/g, function(_, mid) { return mid.trim().split("\n").map(s => s.trim()).join("\n"); }); // join `[/list]` (happens for `- item \n second line\nOther text`) text = text.replace(/\[\/list]((?:\n[ \t]+.*)+)/g, function(_, mid) { var out = mid.trim().split("\n").map(s => s.trim()).join("\n") + "\n[/list]"; return out; }); ```

However, I also came to a realization that trying to convert Markdown to BB Code with regular expressions has inherent limitations (e.g. all 3 tools handle multi-line list items variously incorrectly because that's not an easy thing to catch with a regular expression) so I wrote a userscript that generates BB Code by traversing DOM that was generated from Markdown, and that works better for my purposes