Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make wikilinks_title_after_pipe override wikilinks_title_before_pipe #500

Merged
merged 4 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ clap = { version = "4.0", optional = true, features = [
"string",
"wrap_help",
] }

[[example]]
name = "syntect"
required-features = [ "syntect" ]
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, 'c> CommonMarkFormatter<'a, 'o, 'c> {
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
2 changes: 2 additions & 0 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ pub enum NodeValue {
MultilineBlockQuote(NodeMultilineBlockQuote),

/// **Inline**. A character that has been [escaped](https://github.github.com/gfm/#backslash-escapes)
///
/// Enabled with [`escaped_char_spans`](crate::RenderOptionsBuilder::escaped_char_spans).
Escaped,

/// **Inline**. A wikilink to some 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, 'c> Subject<'a, 'r, 'o, 'd, 'i, 'c> {

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, 'c> Subject<'a, 'r, 'o, 'd, 'i, 'c> {
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
37 changes: 37 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<'c> {
/// [[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<'c> {
/// ```` 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<'c> {
pub link_url_rewriter: Option<Arc<dyn URLRewriter + 'c>>,
}

impl<'c> ExtensionOptions<'c> {
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 Expand Up @@ -865,6 +900,8 @@ pub struct RenderOptions {
/// let xml = markdown_to_commonmark_xml(input, &options);
/// assert!(xml.contains("<text sourcepos=\"1:4-1:15\" xml:space=\"preserve\">"));
/// ```
///
/// [`experimental_inline_sourcepos`]: crate::RenderOptionsBuilder::experimental_inline_sourcepos
#[builder(default)]
pub sourcepos: bool,

Expand Down
Loading