mirror of
https://github.com/discourse/discourse.git
synced 2024-12-15 00:23:49 +08:00
53b3d2f0dc
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
133 lines
4.0 KiB
Ruby
133 lines
4.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
def generate_html(text, opts = {})
|
|
output = "<p><span"
|
|
output += " class=\"discourse-local-date\""
|
|
output += " data-date=\"#{opts[:date]}\"" if opts[:date]
|
|
output += " data-email-preview=\"#{opts[:email_preview]}\"" if opts[:email_preview]
|
|
output += " data-format=\"#{opts[:format]}\"" if opts[:format]
|
|
output += " data-time=\"#{opts[:time]}\"" if opts[:time]
|
|
output += " data-timezone=\"#{opts[:timezone]}\"" if opts[:timezone]
|
|
output += " data-timezones=\"#{opts[:timezones]}\"" if opts[:timezones]
|
|
output += ">"
|
|
output += text
|
|
output + "</span></p>"
|
|
end
|
|
|
|
RSpec.describe PrettyText do
|
|
before { freeze_time }
|
|
|
|
describe "emails simplified rendering" do
|
|
it "works with default markup" do
|
|
cooked = PrettyText.cook("[date=2018-05-08]")
|
|
cooked_mail =
|
|
generate_html(
|
|
"2018-05-08T00:00:00Z UTC",
|
|
date: "2018-05-08",
|
|
email_preview: "2018-05-08T00:00:00Z UTC",
|
|
)
|
|
|
|
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
|
end
|
|
|
|
it "works with time" do
|
|
cooked = PrettyText.cook("[date=2018-05-08 time=20:00:00]")
|
|
cooked_mail =
|
|
generate_html(
|
|
"2018-05-08T20:00:00Z UTC",
|
|
date: "2018-05-08",
|
|
email_preview: "2018-05-08T20:00:00Z UTC",
|
|
time: "20:00:00",
|
|
)
|
|
|
|
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
|
end
|
|
|
|
it "works with multiple timezones" do
|
|
cooked =
|
|
PrettyText.cook(
|
|
'[date=2023-05-08 timezone="Europe/Paris" timezones="America/Los_Angeles|Pacific/Auckland"]',
|
|
)
|
|
cooked_mail =
|
|
generate_html(
|
|
"2023-05-07T22:00:00Z UTC",
|
|
date: "2023-05-08",
|
|
email_preview: "2023-05-07T22:00:00Z UTC",
|
|
timezone: "Europe/Paris",
|
|
timezones: "America/Los_Angeles|Pacific/Auckland",
|
|
)
|
|
|
|
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
|
end
|
|
|
|
describe "discourse_local_dates_email_format" do
|
|
before { SiteSetting.discourse_local_dates_email_format = "DD/MM" }
|
|
|
|
it "uses the site setting" do
|
|
cooked = PrettyText.cook("[date=2018-05-08]")
|
|
cooked_mail = generate_html("08/05 UTC", date: "2018-05-08", email_preview: "08/05 UTC")
|
|
|
|
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "excerpt simplified rendering" do
|
|
let(:post) do
|
|
Fabricate(
|
|
:post,
|
|
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone="America/New_York"]',
|
|
)
|
|
end
|
|
|
|
it "adds UTC" do
|
|
excerpt = PrettyText.excerpt(post.cooked, 200)
|
|
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
|
end
|
|
end
|
|
|
|
describe "special quotes" do
|
|
it "converts special quotes to regular quotes" do
|
|
# german
|
|
post =
|
|
Fabricate(
|
|
:post,
|
|
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=„America/New_York“]',
|
|
)
|
|
excerpt = PrettyText.excerpt(post.cooked, 200)
|
|
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
|
|
|
# french
|
|
post =
|
|
Fabricate(
|
|
:post,
|
|
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=«America/New_York»]',
|
|
)
|
|
excerpt = PrettyText.excerpt(post.cooked, 200)
|
|
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
|
|
|
post =
|
|
Fabricate(
|
|
:post,
|
|
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=“America/New_York”]',
|
|
)
|
|
excerpt = PrettyText.excerpt(post.cooked, 200)
|
|
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
|
end
|
|
end
|
|
|
|
describe "french quotes" do
|
|
let(:post) do
|
|
Fabricate(
|
|
:post,
|
|
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=«America/New_York»]',
|
|
)
|
|
end
|
|
|
|
it "converts french quotes to regular quotes" do
|
|
excerpt = PrettyText.excerpt(post.cooked, 200)
|
|
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
|
end
|
|
end
|
|
end
|