Skip to content

Commit

Permalink
Add support for [code]
Browse files Browse the repository at this point in the history
Add support for the [code] bbcode, making use of the new plain_text config option.

The code block is converted to be wrapped in "```" (on its own line), and crucially the block contents are unaltered by any further parsing.

TODO: In fact it will still convert ">" to ">" and other html entities, which ideally it wouldn't, but when calling with `bbcode_to_md(false)` this is disactivated.

TODO: We can pass a language to achieve syntax highlighting, such as [code=javascript] in some bbcode flavours, and in the bbcode.org reference. This could be converted to "```javascript" in some markdown flavours. Currently it will just ignore the language information and convert a plain "```" block.
  • Loading branch information
harry-wood committed Feb 19, 2023
1 parent 0be2cb2 commit 6d7c8c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/tags/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ module Tags
:html_open => '~~', :html_close => '~~',
:description => 'Strike-through text',
:example => 'This is <s>strike-through text</s>.'},
:code => {
:html_open => "\n```\n", :html_close => "\n```\n",
:description => 'Code block',
:example => '[code]code[/code].',
:plain_tag => true},
:center => {
:html_open => '', :html_close => '',
:description => 'Center a text',
Expand Down
8 changes: 8 additions & 0 deletions test/ruby_bbcode_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def test_color
def test_center
assert_equal 'centered', '[center]centered[/center]'.bbcode_to_md
end

def test_code
assert_equal "\n```\ncode\n```\n", '[code]code[/code]'.bbcode_to_md
assert_equal "\n```\ncode\n```\n", '[code=javascript]code[/code]'.bbcode_to_md
assert_equal "\n```\nSome [b]bbcode[/b] :-)\n```\n", '[code]Some [b]bbcode[/b] :-)[/code]'.bbcode_to_md
assert_equal "\n```\nSome &lt;b&gt;html&lt;/b&gt;\n```\n", '[code]Some <b>html</b>[/code]'.bbcode_to_md
assert_equal "\n```\nSome <b>html</b>\n```\n", '[code]Some <b>html</b>[/code]'.bbcode_to_md(false)
end

def test_ordered_list
assert_equal "\n 1. item 1\n 1. item 2\n\n", '[ol][li]item 1[/li][li]item 2[/li][/ol]'.bbcode_to_md
Expand Down

0 comments on commit 6d7c8c8

Please sign in to comment.