From 9a8b57d21fb0b97a6ec3156deda46d31768a10f4 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Tue, 30 Apr 2024 13:21:36 -0700 Subject: [PATCH] Add `relaxed_autolinks` --- README.md | 9 +++++---- ext/commonmarker/src/options.rs | 4 ++++ lib/commonmarker/config.rb | 1 + test/relaxed_autolinks_test.rb | 23 +++++++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 test/relaxed_autolinks_test.rb diff --git a/README.md b/README.md index 6a14ae75..fdb6837b 100644 --- a/README.md +++ b/README.md @@ -151,10 +151,11 @@ Note that there is a distinction in comrak for "parse" options and "render" opti ### Parse options -| Name | Description | Default | -| --------------------- | ------------------------------------------------------------------------------------ | ------- | -| `smart` | Punctuation (quotes, full-stops and hyphens) are converted into 'smart' punctuation. | `false` | -| `default_info_string` | The default info string for fenced code blocks. | `""` | +| Name | Description | Default | +| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | ------- | +| `smart` | Punctuation (quotes, full-stops and hyphens) are converted into 'smart' punctuation. | `false` | +| `default_info_string` | The default info string for fenced code blocks. | `""` | +| `relaxed_autolinks` | Enable relaxing of the autolink extension parsing, allowing links to be recognized when in brackets, as well as permitting any url scheme. | `false` | ### Render options diff --git a/ext/commonmarker/src/options.rs b/ext/commonmarker/src/options.rs index 7c96a4dc..73a3a0eb 100644 --- a/ext/commonmarker/src/options.rs +++ b/ext/commonmarker/src/options.rs @@ -10,6 +10,7 @@ use crate::utils::try_convert_string; const PARSE_SMART: &str = "smart"; const PARSE_DEFAULT_INFO_STRING: &str = "default_info_string"; +const PARSE_RELAXED_AUTOLINKS: &str = "relaxed_autolinks"; fn iterate_parse_options(comrak_options: &mut ComrakOptions, options_hash: RHash) { options_hash @@ -21,6 +22,9 @@ fn iterate_parse_options(comrak_options: &mut ComrakOptions, options_hash: RHash Ok(Cow::Borrowed(PARSE_DEFAULT_INFO_STRING)) => { comrak_options.parse.default_info_string = try_convert_string(value); } + Ok(Cow::Borrowed(PARSE_RELAXED_AUTOLINKS)) => { + comrak_options.parse.relaxed_autolinks = TryConvert::try_convert(value)?; + } _ => {} } Ok(ForEach::Continue) diff --git a/lib/commonmarker/config.rb b/lib/commonmarker/config.rb index 3db3ac4a..f72353fc 100644 --- a/lib/commonmarker/config.rb +++ b/lib/commonmarker/config.rb @@ -8,6 +8,7 @@ module Config parse: { smart: false, default_info_string: "", + relaxed_autolinks: false, }.freeze, render: { hardbreaks: true, diff --git a/test/relaxed_autolinks_test.rb b/test/relaxed_autolinks_test.rb new file mode 100644 index 00000000..24bbe1b4 --- /dev/null +++ b/test/relaxed_autolinks_test.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require "test_helper" + +class RelaxedAutolinksTest < Minitest::Test + def test_to_html + md = <<~MARKDOWN + [https://foo.com] + + smb:///Volumes/shared/foo.pdf + + rdar://localhost.com/blah + MARKDOWN + + expected = <<~HTML +

[https://foo.com]

+

smb:///Volumes/shared/foo.pdf

+

rdar://localhost.com/blah

+ HTML + + assert_equal(expected, Commonmarker.to_html(md, options: { parse: { relaxed_autolinks: true } })) + end +end