Rails 7 renders accurate plain text output for nested fields

ActionText is a power-packed tool to easily build WYSIWYG editors for your Rails applications. It comes with an array of editor options and transformers. One of them is the Plain Text transformer which converts HTML input to plain text output.

Before

As with any editors, some wonkiness is expected! When using to_plain_text on an ActionText content containing nested lists, the output does not meet expectations. For example, when using indentations to indicate nested list levels, converting to plain text does not preserve the indentation.

ActionText::Content.new("<ul><li>Item 1<ul><li>Item a</li></ul></li></ul>").to_plain_text

=> "• Item 1• Item a"

However, the expected output is close to:

 Item 1
   Item a

After

With recent changes in Rails 7 ActionText, it adds a two-space indentation per nested level for ul and ol when using to_plain_text on action text content!

ActionText::Content.new("<ul><li>Item 0</li><li>Item 1<ul><li>Item A<ol><li>Item i</li><li>Item ii</li></ol></li><li>Item B<ul><li>Item i</li></ul></li></ul></li><li>Item 2</li></ul>").to_plain_text

=> "• Item 0\n• Item 1\n  • Item A\n    1. Item i\n    2. Item ii\n  • Item B\n    • Item i\n• Item 2"

Which looks like,

 Item 0
 Item 1
   Item A
    1. Item i
    2. Item ii
   Item B
     Item i
 Item 2

To know more about the fix checkout this PR.

Need help on your Ruby on Rails or React project?

Join Our Newsletter