Project

General

Profile

Feature #42043 » 0002-Abbreviate-quoted-text-in-descriptions-in-Activity-v.patch

Go MAEDA, 2024-12-26 04:38

View differences:

app/helpers/application_helper.rb
414 414
  end
415 415

  
416 416
  def format_activity_description(text)
417
    h(text.to_s.truncate(240).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')).
418
      gsub(/[\r\n]+/, "<br />").html_safe
417
    h(
418
      # Limit input to avoid regex performance issues
419
      text.to_s.slice(0, 10240)
420
      # Abbreviate consecutive quoted lines as '> ...'
421
      .gsub(%r{(^>.*?(?:\r?\n))(?:>.*?(?:\r?\n)+)+}m, "\\1> ...\n")
422
      # Remove all content following the first <pre> or <code> tag
423
      .sub(%r{[\r\n]*<(pre|code)>.*$}m, '')
424
      # Truncate the description to a specified length and append '...'
425
      .truncate(240)
426
    ).gsub(/[\r\n]+/, "<br>").html_safe
419 427
  end
420 428

  
421 429
  def format_version_name(version)
test/helpers/application_helper_test.rb
2329 2329
    end
2330 2330
  end
2331 2331

  
2332
  def test_format_activity_description_should_strip_quoted_text
2333
    text = <<~TEXT
2334
      John Smith wrote in #note-1:
2335
      > The quick brown fox
2336
      > jumps over the lazy dog.
2337

  
2338
      Brick quiz whangs jumpy veldt fox.
2339

  
2340
      > The five
2341

  
2342
      > boxing wizards
2343

  
2344
      > jump quickly.
2345

  
2346
      The quick onyx goblin jumps over the lazy dwarf.
2347
    TEXT
2348

  
2349
    expected =
2350
      'John Smith wrote in #note-1:<br>' \
2351
      '&gt; The quick brown fox<br>' \
2352
      '&gt; ...<br>' \
2353
      'Brick quiz whangs jumpy veldt fox.<br>' \
2354
      '&gt; The five<br>' \
2355
      '&gt; ...<br>' \
2356
      'The quick onyx goblin jumps over the lazy dwarf.<br>'
2357

  
2358
    assert_equal expected, format_activity_description(text)
2359
  end
2360

  
2332 2361
  private
2333 2362

  
2334 2363
  def wiki_links_with_special_characters
(4-4/5)