Skip to content

Commit

Permalink
Make wikilinks_title_after_pipe override wikilinks_title_before_pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWilsn committed Dec 6, 2024
1 parent c79f715 commit 0660ac9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::nodes::{
use crate::nodes::{NodeList, TableAlignment};
#[cfg(feature = "shortcodes")]
use crate::parser::shortcodes::NodeShortCode;
use crate::parser::Options;
use crate::parser::{Options, WikiLinksMode};
use crate::scanners;
use crate::strings::trim_start_match;
use crate::{nodes, Plugins};
Expand Down Expand Up @@ -761,12 +761,12 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
fn format_wikilink(&mut self, nl: &NodeWikiLink, entering: bool) -> bool {
if entering {
write!(self, "[[").unwrap();
if self.options.extension.wikilinks_title_after_pipe {
if self.options.extension.wikilinks() == Some(WikiLinksMode::UrlFirst) {
self.output(nl.url.as_bytes(), false, Escaping::Url);
write!(self, "|").unwrap();
}
} else {
if self.options.extension.wikilinks_title_before_pipe {
if self.options.extension.wikilinks() == Some(WikiLinksMode::TitleFirst) {
write!(self, "|").unwrap();
self.output(nl.url.as_bytes(), false, Escaping::Url);
}
Expand Down
17 changes: 9 additions & 8 deletions src/parser/inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use std::str;
use typed_arena::Arena;
use unicode_categories::UnicodeCategories;

use super::WikiLinksMode;

const MAXBACKTICKS: usize = 80;
const MAX_LINK_LABEL_LENGTH: usize = 1000;
const MAX_MATH_DOLLARS: usize = 2;
Expand Down Expand Up @@ -235,8 +237,7 @@ impl<'a, 'r, 'o, 'd, 'i> Subject<'a, 'r, 'o, 'd, 'i> {

let mut wikilink_inl = None;

if (self.options.extension.wikilinks_title_after_pipe
|| self.options.extension.wikilinks_title_before_pipe)
if self.options.extension.wikilinks().is_some()
&& !self.within_brackets
&& self.peek_char() == Some(&(b'['))
{
Expand Down Expand Up @@ -1804,16 +1805,16 @@ impl<'a, 'r, 'o, 'd, 'i> Subject<'a, 'r, 'o, 'd, 'i> {
if self.peek_char() == Some(&(b']')) && self.peek_char_n(1) == Some(&(b']')) {
self.pos += 2;

if self.options.extension.wikilinks_title_after_pipe {
Some(WikilinkComponents {
match self.options.extension.wikilinks() {
Some(WikiLinksMode::UrlFirst) => Some(WikilinkComponents {
url: left,
link_label: Some((right, right_startpos + 1, self.pos - 3)),
})
} else {
Some(WikilinkComponents {
}),
Some(WikiLinksMode::TitleFirst) => Some(WikilinkComponents {
url: right,
link_label: Some((left, left_startpos + 1, right_startpos - 1)),
})
}),
None => unreachable!(),
}
} else {
self.pos = left_startpos;
Expand Down
35 changes: 35 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ where
}
}

#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
/// Selects between wikilinks with the title first or the URL first.
pub(crate) enum WikiLinksMode {
/// Indicates that the URL precedes the title. For example: `[[http://example.com|link
/// title]]`.
UrlFirst,

/// Indicates that the title precedes the URL. For example: `[[link title|http://example.com]]`.
TitleFirst,
}

#[non_exhaustive]
#[derive(Default, Debug, Clone, Builder)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
Expand Down Expand Up @@ -472,6 +485,11 @@ pub struct ExtensionOptions {
/// [[url|link label]]
/// ````
///
/// When both this option and [`wikilinks_title_before_pipe`][0] are enabled, this option takes
/// precedence.
///
/// [0]: Self::wikilinks_title_before_pipe
///
/// ```
/// # use comrak::{markdown_to_html, Options};
/// let mut options = Options::default();
Expand All @@ -487,6 +505,10 @@ pub struct ExtensionOptions {
/// ```` md
/// [[link label|url]]
/// ````
/// When both this option and [`wikilinks_title_after_pipe`][0] are enabled,
/// [`wikilinks_title_after_pipe`][0] takes precedence.
///
/// [0]: Self::wikilinks_title_after_pipe
///
/// ```
/// # use comrak::{markdown_to_html, Options};
Expand Down Expand Up @@ -617,6 +639,19 @@ pub struct ExtensionOptions {
pub link_url_rewriter: Option<Arc<dyn URLRewriter>>,
}

impl ExtensionOptions {
pub(crate) fn wikilinks(&self) -> Option<WikiLinksMode> {
match (
self.wikilinks_title_before_pipe,
self.wikilinks_title_after_pipe,
) {
(false, false) => None,
(true, false) => Some(WikiLinksMode::TitleFirst),
(_, _) => Some(WikiLinksMode::UrlFirst),
}
}
}

#[non_exhaustive]
#[derive(Default, Clone, Debug, Builder)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
Expand Down

0 comments on commit 0660ac9

Please sign in to comment.