Skip to content
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
31 changes: 30 additions & 1 deletion crates/quarto-markdown-pandoc/src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pub struct Filter<'a> {
pub shortcode: InlineFilterField<'a, pandoc::Shortcode>,
pub note_reference: InlineFilterField<'a, pandoc::NoteReference>,
pub attr: InlineFilterField<'a, pandoc::Attr>,
pub insert: InlineFilterField<'a, pandoc::Insert>,
pub delete: InlineFilterField<'a, pandoc::Delete>,
pub highlight: InlineFilterField<'a, pandoc::Highlight>,
pub edit_comment: InlineFilterField<'a, pandoc::EditComment>,

pub paragraph: BlockFilterField<'a, pandoc::Paragraph>,
pub plain: BlockFilterField<'a, pandoc::Plain>,
Expand Down Expand Up @@ -119,6 +123,11 @@ impl Default for Filter<'static> {
horizontal_rule: None,
attr: None,

insert: None,
delete: None,
highlight: None,
edit_comment: None,

meta: None,
}
}
Expand Down Expand Up @@ -200,6 +209,10 @@ define_filter_with_methods!(
shortcode,
note_reference,
attr,
insert,
delete,
highlight,
edit_comment
);

define_filter_with_methods!(
Expand Down Expand Up @@ -300,7 +313,11 @@ impl_inline_filterable_simple!(
Quoted,
Link,
Image,
Span
Span,
Insert,
Delete,
Highlight,
EditComment
);

impl InlineFilterableStructure for pandoc::Note {
Expand Down Expand Up @@ -617,6 +634,18 @@ pub fn topdown_traverse_inline(inline: Inline, filter: &mut Filter) -> Inlines {
Inline::Attr(attr) => {
handle_inline_filter!(Attr, attr, attr, filter)
}
Inline::Insert(ins) => {
handle_inline_filter!(Insert, ins, insert, filter)
}
Inline::Delete(del) => {
handle_inline_filter!(Delete, del, delete, filter)
}
Inline::Highlight(hl) => {
handle_inline_filter!(Highlight, hl, highlight, filter)
}
Inline::EditComment(ec) => {
handle_inline_filter!(EditComment, ec, edit_comment, filter)
}
}
}

Expand Down
32 changes: 31 additions & 1 deletion crates/quarto-markdown-pandoc/src/pandoc/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ pub enum Inline {
// this is used to represent commonmark attributes in the document in places
// where they are not directly attached to a block, like in headings and tables
Attr(Attr),

// CriticMarkup-like extensions
Insert(Insert),
Delete(Delete),
Highlight(Highlight),
EditComment(EditComment),
}

pub type Inlines = Vec<Inline>;
Expand Down Expand Up @@ -194,6 +200,26 @@ pub enum CitationMode {
NormalCitation,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Insert {
pub content: Inlines,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Delete {
pub content: Inlines,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Highlight {
pub content: Inlines,
}

#[derive(Debug, Clone, PartialEq)]
pub struct EditComment {
pub content: Inlines,
}

impl_source_location!(Space, LineBreak, SoftBreak);

pub trait AsInline {
Expand Down Expand Up @@ -241,7 +267,11 @@ impl_as_inline!(
Span,
Shortcode,
NoteReference,
Attr
Attr,
Insert,
Delete,
Highlight,
EditComment
);

pub fn is_empty_target(target: &Target) -> bool {
Expand Down
7 changes: 4 additions & 3 deletions crates/quarto-markdown-pandoc/src/pandoc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ pub use crate::pandoc::block::{
};
pub use crate::pandoc::caption::Caption;
pub use crate::pandoc::inline::{
Citation, CitationMode, Cite, Code, Emph, Image, Inline, Inlines, LineBreak, Link, Math,
MathType, Note, NoteReference, QuoteType, Quoted, RawInline, SmallCaps, SoftBreak, Space, Span,
Str, Strikeout, Strong, Subscript, Superscript, Underline,
Citation, CitationMode, Cite, Code, Delete, EditComment, Emph, Highlight, Image, Inline,
Inlines, Insert, LineBreak, Link, Math, MathType, Note, NoteReference, QuoteType, Quoted,
RawInline, SmallCaps, SoftBreak, Space, Span, Str, Strikeout, Strong, Subscript, Superscript,
Underline,
};
pub use crate::pandoc::list::{ListAttributes, ListNumberDelim, ListNumberStyle};
pub use crate::pandoc::pandoc::Pandoc;
Expand Down
109 changes: 105 additions & 4 deletions crates/quarto-markdown-pandoc/src/pandoc/treesitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use crate::pandoc::block::{
};
use crate::pandoc::caption::Caption;
use crate::pandoc::inline::{
Citation, CitationMode, Cite, Code, Emph, Image, Inline, Inlines, LineBreak, Link, Math,
MathType, Note, NoteReference, QuoteType, Quoted, RawInline, SoftBreak, Space, Span, Str,
Strikeout, Strong, Subscript, Superscript, Target, is_empty_target,
Citation, CitationMode, Cite, Code, Delete, EditComment, Emph, Highlight, Image, Inline,
Inlines, Insert, LineBreak, Link, Math, MathType, Note, NoteReference, QuoteType, Quoted,
RawInline, SoftBreak, Space, Span, Str, Strikeout, Strong, Subscript, Superscript, Target,
is_empty_target,
};

use crate::pandoc::inline::{make_cite_inline, make_span_inline};
Expand Down Expand Up @@ -894,7 +895,11 @@ fn native_visitor<T: Write>(
| "superscript_delimiter"
| "subscript_delimiter"
| "strikeout_delimiter"
| "emphasis_delimiter" => {
| "emphasis_delimiter"
| "insert_delimiter"
| "delete_delimiter"
| "highlight_delimiter"
| "edit_comment_delimiter" => {
PandocNativeIntermediate::IntermediateUnknown(node_location(node))
}
"soft_line_break" => {
Expand Down Expand Up @@ -971,6 +976,46 @@ fn native_visitor<T: Write>(
content: inlines,
}))
}
"insert" => {
let inlines: Vec<_> = children
.into_iter()
.filter(|(node, _)| node != "insert_delimiter")
.map(native_inline)
.collect();
PandocNativeIntermediate::IntermediateInline(Inline::Insert(Insert {
content: inlines,
}))
}
"delete" => {
let inlines: Vec<_> = children
.into_iter()
.filter(|(node, _)| node != "delete_delimiter")
.map(native_inline)
.collect();
PandocNativeIntermediate::IntermediateInline(Inline::Delete(Delete {
content: inlines,
}))
}
"highlight" => {
let inlines: Vec<_> = children
.into_iter()
.filter(|(node, _)| node != "highlight_delimiter")
.map(native_inline)
.collect();
PandocNativeIntermediate::IntermediateInline(Inline::Highlight(Highlight {
content: inlines,
}))
}
"edit_comment" => {
let inlines: Vec<_> = children
.into_iter()
.filter(|(node, _)| node != "edit_comment_delimiter")
.map(native_inline)
.collect();
PandocNativeIntermediate::IntermediateInline(Inline::EditComment(EditComment {
content: inlines,
}))
}

"quoted_span" => {
let mut quote_type = QuoteType::SingleQuote;
Expand Down Expand Up @@ -1895,6 +1940,62 @@ fn desugar(doc: Pandoc) -> Result<Pandoc, Vec<String>> {
false,
)
})
.with_insert(|insert| {
let (content, _changed) = trim_inlines(insert.content);
FilterResult(
vec![Inline::Span(Span {
attr: (
"".to_string(),
vec!["quarto-insert".to_string()],
HashMap::new(),
),
content,
})],
true,
)
})
.with_delete(|delete| {
let (content, _changed) = trim_inlines(delete.content);
FilterResult(
vec![Inline::Span(Span {
attr: (
"".to_string(),
vec!["quarto-delete".to_string()],
HashMap::new(),
),
content,
})],
true,
)
})
.with_highlight(|highlight| {
let (content, _changed) = trim_inlines(highlight.content);
FilterResult(
vec![Inline::Span(Span {
attr: (
"".to_string(),
vec!["quarto-highlight".to_string()],
HashMap::new(),
),
content,
})],
true,
)
})
.with_edit_comment(|edit_comment| {
let (content, _changed) = trim_inlines(edit_comment.content);
FilterResult(
vec![Inline::Span(Span {
attr: (
"".to_string(),
vec!["quarto-edit-comment".to_string()],
HashMap::new(),
),
content,
})],
true,
)
})
.with_inlines(|inlines| {
let mut result = vec![];
// states in this state machine:
Expand Down
8 changes: 7 additions & 1 deletion crates/quarto-markdown-pandoc/src/writers/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ fn write_inline(inline: &Inline) -> Value {
})
}).collect::<Vec<_>>()
}),
Inline::Shortcode(_) | Inline::NoteReference(_) | Inline::Attr(_) => {
Inline::Shortcode(_)
| Inline::NoteReference(_)
| Inline::Attr(_)
| Inline::Insert(_)
| Inline::Delete(_)
| Inline::Highlight(_)
| Inline::EditComment(_) => {
panic!("Unsupported inline type: {:?}", inline)
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/quarto-markdown-pandoc/tests/snapshots/013.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[++insertions with markdown _inside_ them], [--deletions, **likewise**], [!! highlight this part][>> with a comment about the previous highlight].
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ Para [Span ( "" , ["quarto-insert"] , [] ) [Str "insertions", Space, Str "with", Space, Str "markdown", Space, Emph [Str "inside"], Space, Str "them"], Str ",", Space, Span ( "" , ["quarto-delete"] , [] ) [Str "deletions,", Space, Strong [Str "likewise"]], Str ",", Space, Span ( "" , ["quarto-highlight"] , [] ) [Str "highlight", Space, Str "this", Space, Str "part"], Span ( "" , ["quarto-edit-comment"] , [] ) [Str "with", Space, Str "a", Space, Str "comment", Space, Str "about", Space, Str "the", Space, Str "previous", Space, Str "highlight"], Str "."] ]
28 changes: 28 additions & 0 deletions crates/tree-sitter-qmd/tree-sitter-markdown-inline/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,30 @@ module.exports = grammar(add_inline_rules({
alias($._latex_span_close, $.latex_span_delimiter),
),

insert: $ => seq(
prec(3, alias(/\[\+\+[ ]*/, $.insert_delimiter)),
repeat($._inline_element),
prec(3, alias(/[ ]*\]/, $.insert_delimiter)),
),

delete: $ => seq(
prec(3, alias(/\[\-\-[ ]*/, $.delete_delimiter)),
repeat($._inline_element),
prec(3, alias(/[ ]*\]/, $.delete_delimiter)),
),

highlight: $ => seq(
prec(3, alias(/\[\!\![ ]*/, $.highlight_delimiter)),
repeat($._inline_element),
prec(3, alias(/[ ]*\]/, $.highlight_delimiter)),
),

edit_comment: $ => seq(
prec(3, alias(/\[>>[ ]*/, $.edit_comment_delimiter)),
repeat($._inline_element),
prec(3, alias(/[ ]*\]/, $.edit_comment_delimiter)),
),

superscript: $ => seq(
alias($._superscript_open, $.superscript_delimiter),
repeat($._inline_element),
Expand Down Expand Up @@ -399,6 +423,10 @@ module.exports = grammar(add_inline_rules({
$.shortcode_escaped,
$.note_reference,
$.commonmark_attribute,
$.insert,
$.delete,
$.highlight,
$.edit_comment,

alias($._text_base, $.text_base),
$._unclosed_span,
Expand Down
Loading
Loading