FIX: BBCode tag parser

Wasn't quite handling the cases where a closing bracket `]` was used in the value of one of the attributes.

```markdown
[chat quote=user channel="[broken]"]
```

Would not be correctly parsed because we would _greedily_ use the first `]` as the end of the tag even though it might be a valid character when inside proper quotes.

c39a4de139/app/assets/javascripts/discourse-markdown-it/src/features/bbcode-block.js (L62)

Re-wrote the `parseBBCodeTag` to properly handle the following cases

- A closing tag (aka `[/name]`) which are easy since they don't have any attributes
- An old `[quote=...]` format we used that doesn't uses quotes but still has various attributes of the form `key:value`
- All three valid BBCode opening tag formats we support
  - `[name]` without any attributes
  - `[name=foo]` with a default value
  - `[name foo=bar]` with some attributes

Ended up having to fix/rewrite the few bbcode rules that were using the `parseBBCodeTag` function, namely `d-wrap` and `discourse-local-dates`.

While working on this, I think I also found a way to get rid the of shims we had in place so that plugins could use the `parseBBCodeTag` function.

Reference - https://meta.discourse.org/t/having-a-right-bracket-in-a-channel-name-breaks-all-quotes-from-that-channel/308439
This commit is contained in:
Régis Hanol
2024-05-24 10:47:45 +02:00
parent 2393234be5
commit 53b3d2f0dc
11 changed files with 250 additions and 364 deletions

View File

@ -1906,13 +1906,12 @@ RSpec.describe CookedPostProcessor do
end
context "with an unmodified quote" do
let(:cp) do
Fabricate(
:post,
raw:
"[quote=\"#{pp.user.username}, post: #{pp.post_number}, topic:#{pp.topic_id}]\nripe for quoting\n[/quote]\ntest",
)
end
let(:cp) { Fabricate(:post, raw: <<~MARKDOWN) }
[quote="#{pp.user.username}, post: #{pp.post_number}, topic:#{pp.topic_id}"]
ripe for quoting
[/quote]
test
MARKDOWN
it "should not be marked as modified" do
cpp.post_process_quotes
@ -1921,13 +1920,12 @@ RSpec.describe CookedPostProcessor do
end
context "with a modified quote" do
let(:cp) do
Fabricate(
:post,
raw:
"[quote=\"#{pp.user.username}, post: #{pp.post_number}, topic:#{pp.topic_id}]\nmodified\n[/quote]\ntest",
)
end
let(:cp) { Fabricate(:post, raw: <<~MARKDOWN) }
[quote="#{pp.user.username}, post: #{pp.post_number}, topic:#{pp.topic_id}"]
modified
[/quote]
test
MARKDOWN
it "should be marked as modified" do
cpp.post_process_quotes
@ -1936,13 +1934,12 @@ RSpec.describe CookedPostProcessor do
end
context "with external discourse instance quote" do
let(:external_raw) { <<~RAW.strip }
let(:cp) { Fabricate(:post, user: user_with_auto_groups, raw: <<~MARKDOWN.strip) }
[quote="random_guy_not_from_our_discourse, post:2004, topic:401"]
this quote is not from our discourse
[/quote]
and this is a reply
RAW
let(:cp) { Fabricate(:post, user: user_with_auto_groups, raw: external_raw) }
MARKDOWN
it "it should be marked as missing" do
cpp.post_process_quotes