Skip to content

Commit

Permalink
support wiki
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian committed May 29, 2024
1 parent 7b1ff6c commit 254ad33
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 45 deletions.
95 changes: 51 additions & 44 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion ext/commonmarker/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use comrak::nodes::{
Ast as ComrakAst, AstNode as ComrakAstNode, ListDelimType, ListType, NodeCode, NodeCodeBlock,
NodeDescriptionItem, NodeFootnoteDefinition, NodeFootnoteReference, NodeHeading, NodeHtmlBlock,
NodeLink, NodeList, NodeMath, NodeMultilineBlockQuote, NodeShortCode, NodeTable,
NodeValue as ComrakNodeValue, TableAlignment,
NodeValue as ComrakNodeValue, NodeWikiLink, TableAlignment,
};
use comrak::{arena_tree::Node as ComrakNode, ComrakOptions};
use magnus::RArray;
Expand Down Expand Up @@ -453,6 +453,15 @@ impl CommonmarkerNode {
}

"escaped" => ComrakNodeValue::Escaped,

"wikilink" => {
let kwargs =
scan_args::get_kwargs::<_, (String,), (), ()>(args.keywords, &["url"], &[])?;

let (url,) = kwargs.required;

ComrakNodeValue::WikiLink(NodeWikiLink { url })
}
_ => panic!("unknown node type {}", node_type),
};

Expand Down Expand Up @@ -539,6 +548,7 @@ impl CommonmarkerNode {
ComrakNodeValue::MultilineBlockQuote(_) => Symbol::new("multiline_block_quote"),
ComrakNodeValue::Escaped => Symbol::new("escaped"),
ComrakNodeValue::Math(..) => Symbol::new("math"),
ComrakNodeValue::WikiLink(..) => Symbol::new("wikilink"),
}
}

Expand Down
10 changes: 10 additions & 0 deletions ext/commonmarker/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ const EXTENSION_SHORTCODES: &str = "shortcodes";
const EXTENSION_MULTILINE_BLOCK_QUOTES: &str = "multiline_block_quotes";
const EXTENSION_MATH_DOLLARS: &str = "math_dollars";
const EXTENSION_MATH_CODE: &str = "math_code";
const EXTENSION_WIKILINKS_TITLE_AFTER_PIPE: &str = "wikilinks_title_after_pipe";
const EXTENSION_WIKILINKS_TITLE_BEFORE_PIPE: &str = "wikilinks_title_before_pipe";

fn iterate_extension_options(comrak_options: &mut ComrakOptions, options_hash: RHash) {
options_hash
Expand Down Expand Up @@ -138,6 +140,14 @@ fn iterate_extension_options(comrak_options: &mut ComrakOptions, options_hash: R
Ok(Cow::Borrowed(EXTENSION_MATH_CODE)) => {
comrak_options.extension.math_code = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(EXTENSION_WIKILINKS_TITLE_AFTER_PIPE)) => {
comrak_options.extension.wikilinks_title_after_pipe =
TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(EXTENSION_WIKILINKS_TITLE_BEFORE_PIPE)) => {
comrak_options.extension.wikilinks_title_before_pipe =
TryConvert::try_convert(value)?;
}
_ => {}
}
Ok(ForEach::Continue)
Expand Down
2 changes: 2 additions & 0 deletions lib/commonmarker/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ module Config
multiline_block_quotes: false,
math_dollars: false,
math_code: false,
wikilinks_title_before_pipe: false,
wikilinks_title_after_pipe: false,
},
format: [:html].freeze,
}.freeze
Expand Down
1 change: 1 addition & 0 deletions test/node/creation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def test_it_can_make_all_the_nodes
[:math, dollar_math: true, display_math: false, literal: "$a+b=2$"],
[:multiline_block_quote, fence_length: 2, fence_offset: 0],
[:escaped],
[:wikilink, url: "www.yetto.app/billy.png"],
]
node_types.each do |type, arguments|
node = arguments.nil? ? Commonmarker::Node.new(type) : Commonmarker::Node.new(type, **arguments)
Expand Down
29 changes: 29 additions & 0 deletions test/wikilinks_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "test_helper"

class WikilinksTest < Minitest::Test
def test_works_with_after_pipe
md = <<~MARKDOWN
[[url|link label]]
MARKDOWN

expected = <<~HTML
<p><a href="url" data-wikilink="true">link label</a></p>
HTML

assert_equal(expected, Commonmarker.to_html(md, options: { extension: { wikilinks_title_after_pipe: true } }))
end

def test_works_with_before_pipe
md = <<~MARKDOWN
[[link label|url]]
MARKDOWN

expected = <<~HTML
<p><a href="url" data-wikilink="true">link label</a></p>
HTML

assert_equal(expected, Commonmarker.to_html(md, options: { extension: { wikilinks_title_before_pipe: true } }))
end
end

0 comments on commit 254ad33

Please sign in to comment.