Skip to content

Commit 0660ac9

Browse files
committed
Make wikilinks_title_after_pipe override wikilinks_title_before_pipe
1 parent c79f715 commit 0660ac9

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/cm.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::nodes::{
66
use crate::nodes::{NodeList, TableAlignment};
77
#[cfg(feature = "shortcodes")]
88
use crate::parser::shortcodes::NodeShortCode;
9-
use crate::parser::Options;
9+
use crate::parser::{Options, WikiLinksMode};
1010
use crate::scanners;
1111
use crate::strings::trim_start_match;
1212
use crate::{nodes, Plugins};
@@ -761,12 +761,12 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
761761
fn format_wikilink(&mut self, nl: &NodeWikiLink, entering: bool) -> bool {
762762
if entering {
763763
write!(self, "[[").unwrap();
764-
if self.options.extension.wikilinks_title_after_pipe {
764+
if self.options.extension.wikilinks() == Some(WikiLinksMode::UrlFirst) {
765765
self.output(nl.url.as_bytes(), false, Escaping::Url);
766766
write!(self, "|").unwrap();
767767
}
768768
} else {
769-
if self.options.extension.wikilinks_title_before_pipe {
769+
if self.options.extension.wikilinks() == Some(WikiLinksMode::TitleFirst) {
770770
write!(self, "|").unwrap();
771771
self.output(nl.url.as_bytes(), false, Escaping::Url);
772772
}

src/parser/inlines.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use std::str;
2121
use typed_arena::Arena;
2222
use unicode_categories::UnicodeCategories;
2323

24+
use super::WikiLinksMode;
25+
2426
const MAXBACKTICKS: usize = 80;
2527
const MAX_LINK_LABEL_LENGTH: usize = 1000;
2628
const MAX_MATH_DOLLARS: usize = 2;
@@ -235,8 +237,7 @@ impl<'a, 'r, 'o, 'd, 'i> Subject<'a, 'r, 'o, 'd, 'i> {
235237

236238
let mut wikilink_inl = None;
237239

238-
if (self.options.extension.wikilinks_title_after_pipe
239-
|| self.options.extension.wikilinks_title_before_pipe)
240+
if self.options.extension.wikilinks().is_some()
240241
&& !self.within_brackets
241242
&& self.peek_char() == Some(&(b'['))
242243
{
@@ -1804,16 +1805,16 @@ impl<'a, 'r, 'o, 'd, 'i> Subject<'a, 'r, 'o, 'd, 'i> {
18041805
if self.peek_char() == Some(&(b']')) && self.peek_char_n(1) == Some(&(b']')) {
18051806
self.pos += 2;
18061807

1807-
if self.options.extension.wikilinks_title_after_pipe {
1808-
Some(WikilinkComponents {
1808+
match self.options.extension.wikilinks() {
1809+
Some(WikiLinksMode::UrlFirst) => Some(WikilinkComponents {
18091810
url: left,
18101811
link_label: Some((right, right_startpos + 1, self.pos - 3)),
1811-
})
1812-
} else {
1813-
Some(WikilinkComponents {
1812+
}),
1813+
Some(WikiLinksMode::TitleFirst) => Some(WikilinkComponents {
18141814
url: right,
18151815
link_label: Some((left, left_startpos + 1, right_startpos - 1)),
1816-
})
1816+
}),
1817+
None => unreachable!(),
18171818
}
18181819
} else {
18191820
self.pos = left_startpos;

src/parser/mod.rs

+35
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,19 @@ where
192192
}
193193
}
194194

195+
#[non_exhaustive]
196+
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
197+
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
198+
/// Selects between wikilinks with the title first or the URL first.
199+
pub(crate) enum WikiLinksMode {
200+
/// Indicates that the URL precedes the title. For example: `[[http://example.com|link
201+
/// title]]`.
202+
UrlFirst,
203+
204+
/// Indicates that the title precedes the URL. For example: `[[link title|http://example.com]]`.
205+
TitleFirst,
206+
}
207+
195208
#[non_exhaustive]
196209
#[derive(Default, Debug, Clone, Builder)]
197210
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
@@ -472,6 +485,11 @@ pub struct ExtensionOptions {
472485
/// [[url|link label]]
473486
/// ````
474487
///
488+
/// When both this option and [`wikilinks_title_before_pipe`][0] are enabled, this option takes
489+
/// precedence.
490+
///
491+
/// [0]: Self::wikilinks_title_before_pipe
492+
///
475493
/// ```
476494
/// # use comrak::{markdown_to_html, Options};
477495
/// let mut options = Options::default();
@@ -487,6 +505,10 @@ pub struct ExtensionOptions {
487505
/// ```` md
488506
/// [[link label|url]]
489507
/// ````
508+
/// When both this option and [`wikilinks_title_after_pipe`][0] are enabled,
509+
/// [`wikilinks_title_after_pipe`][0] takes precedence.
510+
///
511+
/// [0]: Self::wikilinks_title_after_pipe
490512
///
491513
/// ```
492514
/// # use comrak::{markdown_to_html, Options};
@@ -617,6 +639,19 @@ pub struct ExtensionOptions {
617639
pub link_url_rewriter: Option<Arc<dyn URLRewriter>>,
618640
}
619641

642+
impl ExtensionOptions {
643+
pub(crate) fn wikilinks(&self) -> Option<WikiLinksMode> {
644+
match (
645+
self.wikilinks_title_before_pipe,
646+
self.wikilinks_title_after_pipe,
647+
) {
648+
(false, false) => None,
649+
(true, false) => Some(WikiLinksMode::TitleFirst),
650+
(_, _) => Some(WikiLinksMode::UrlFirst),
651+
}
652+
}
653+
}
654+
620655
#[non_exhaustive]
621656
#[derive(Default, Clone, Debug, Builder)]
622657
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]

0 commit comments

Comments
 (0)