Skip to content

Commit

Permalink
👌 Only set max indent of 4, if code blocks added
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell authored and rlidwka committed Jul 6, 2023
1 parent fa242c8 commit 78914cd
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 35 deletions.
5 changes: 5 additions & 0 deletions src/parser/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub struct MarkdownIt {
#[doc(hidden)]
pub max_nesting: u32,

/// Maximum allowed indentation for syntax blocks
/// default i32::MAX, indented code blocks will set this to 4
pub max_indent: i32,

ruler: Ruler<TypeKey, RuleFn>,
}

Expand Down Expand Up @@ -77,6 +81,7 @@ impl Default for MarkdownIt {
ext: MarkdownItExtSet::new(),
max_nesting: 100,
ruler: Ruler::new(),
max_indent: i32::MAX,
};
block::builtin::add(&mut md);
inline::builtin::add(&mut md);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/cmark/block/blockquote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub fn add(md: &mut MarkdownIt) {
pub struct BlockquoteScanner;
impl BlockRule for BlockquoteScanner {
fn check(state: &mut BlockState) -> Option<()> {
// if it's indented more than 3 spaces, it should be a code block
if state.line_indent(state.line) >= 4 { return None; }

if state.line_indent(state.line) >= state.md.max_indent { return None; }

// check the block quote marker
let Some('>') = state.get_line(state.line).chars().next() else { return None; };
Expand Down
1 change: 1 addition & 0 deletions src/plugins/cmark/block/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl NodeValue for CodeBlock {

pub fn add(md: &mut MarkdownIt) {
md.block.add_rule::<CodeScanner>();
md.max_indent = 4;
}

#[doc(hidden)]
Expand Down
7 changes: 3 additions & 4 deletions src/plugins/cmark/block/fence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ pub struct FenceScanner;

impl FenceScanner {
fn get_header<'a>(state: &'a mut BlockState) -> Option<(char, usize, &'a str)> {
// if it's indented more than 3 spaces, it should be a code block
if state.line_indent(state.line) >= 4 { return None; }

if state.line_indent(state.line) >= state.md.max_indent { return None; }

let line = state.get_line(state.line);
let mut chars = line.chars();
Expand Down Expand Up @@ -120,8 +120,7 @@ impl BlockRule for FenceScanner {

if Some(marker) != chars.next() { continue; }

if state.line_indent(next_line) >= 4 {
// closing fence should be indented less than 4 spaces
if state.line_indent(next_line) >= state.md.max_indent {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/cmark/block/heading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub fn add(md: &mut MarkdownIt) {
pub struct HeadingScanner;
impl BlockRule for HeadingScanner {
fn run(state: &mut BlockState) -> Option<(Node, usize)> {
// if it's indented more than 3 spaces, it should be a code block
if state.line_indent(state.line) >= 4 { return None; }

if state.line_indent(state.line) >= state.md.max_indent { return None; }

let line = state.get_line(state.line);
let Some('#') = line.chars().next() else { return None; };
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/cmark/block/hr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub fn add(md: &mut MarkdownIt) {
pub struct HrScanner;
impl BlockRule for HrScanner {
fn run(state: &mut BlockState) -> Option<(Node, usize)> {
// if it's indented more than 3 spaces, it should be a code block
if state.line_indent(state.line) >= 4 { return None; }

if state.line_indent(state.line) >= state.md.max_indent { return None; }

let mut chars = state.get_line(state.line).chars();

Expand Down
8 changes: 4 additions & 4 deletions src/plugins/cmark/block/lheading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ impl BlockRule for LHeadingScanner {
}

fn run(state: &mut BlockState) -> Option<(Node, usize)> {
// if it's indented more than 3 spaces, it should be a code block
if state.line_indent(state.line) >= 4 { return None; }

if state.line_indent(state.line) >= state.md.max_indent { return None; }

let start_line = state.line;
let mut next_line = start_line;
Expand All @@ -53,9 +53,9 @@ impl BlockRule for LHeadingScanner {

if next_line >= state.line_max || state.is_empty(next_line) { break; }

// this would be a code block normally, but after paragraph
// this may be a code block normally, but after paragraph
// it's considered a lazy continuation regardless of what's there
if state.line_indent(next_line) >= 4 { continue; }
if state.line_indent(next_line) >= state.md.max_indent { continue; }

//
// Check for underline in setext header
Expand Down
13 changes: 6 additions & 7 deletions src/plugins/cmark/block/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ impl ListScanner {
}

fn find_marker(state: &mut BlockState, silent: bool) -> Option<(usize, Option<u32>, char)> {
// if it's indented more than 3 spaces, it should be a code block
if state.line_indent(state.line) >= 4 { return None; }

if state.line_indent(state.line) >= state.md.max_indent { return None; }

// Special case:
// - item 1
Expand All @@ -142,7 +142,7 @@ impl ListScanner {
// - this one is a paragraph continuation
if let Some(list_indent) = state.list_indent {
let indent_nonspace = state.line_offsets[state.line].indent_nonspace;
if indent_nonspace - list_indent as i32 >= 4 &&
if indent_nonspace - list_indent as i32 >= state.md.max_indent &&
indent_nonspace < state.blk_indent as i32 {
return None;
}
Expand Down Expand Up @@ -253,8 +253,8 @@ impl BlockRule for ListScanner {
if reached_end_of_line {
// trimming space in "- \n 3" case, indent is 1 here
indent_after_marker = 1;
} else if indent_after_marker > 4 {
// If we have more than 4 spaces, the indent is 1
} else if indent_after_marker as i32 > state.md.max_indent {
// If we have more than the max indent, the indent is 1
// (the rest is just indented code block)
indent_after_marker = 1;
}
Expand Down Expand Up @@ -327,8 +327,7 @@ impl BlockRule for ListScanner {
//
if state.line_indent(next_line) < 0 { break; }

// if it's indented more than 3 spaces, it should be a code block
if state.line_indent(next_line) >= 4 { break; }
if state.line_indent(next_line) >= state.md.max_indent { break; }

// fail if terminating block found
if state.test_rules_at_line() { break; }
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/cmark/block/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ impl BlockRule for ParagraphScanner {

if next_line >= state.line_max || state.is_empty(next_line) { break; }

// this would be a code block normally, but after paragraph
// this may be a code block normally, but after paragraph
// it's considered a lazy continuation regardless of what's there
if state.line_indent(next_line) >= 4 { continue; }
if state.line_indent(next_line) >= state.md.max_indent { continue; }

// quirk for blockquotes, this line should already be checked by that rule
if state.line_offsets[next_line].indent_nonspace < 0 { continue; }
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/cmark/block/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ impl BlockRule for ReferenceScanner {
}

fn run(state: &mut BlockState) -> Option<(Node, usize)> {
// if it's indented more than 3 spaces, it should be a code block
if state.line_indent(state.line) >= 4 { return None; }

if state.line_indent(state.line) >= state.md.max_indent { return None; }

let mut chars = state.get_line(state.line).chars();

Expand Down Expand Up @@ -236,9 +236,9 @@ impl BlockRule for ReferenceScanner {

if next_line >= state.line_max || state.is_empty(next_line) { break; }

// this would be a code block normally, but after paragraph
// this may be a code block normally, but after paragraph
// it's considered a lazy continuation regardless of what's there
if state.line_indent(next_line) >= 4 { continue; }
if state.line_indent(next_line) >= state.md.max_indent { continue; }

// quirk for blockquotes, this line should already be checked by that rule
if state.line_offsets[next_line].indent_nonspace < 0 { continue; }
Expand Down
9 changes: 3 additions & 6 deletions src/plugins/extra/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,12 @@ impl TableScanner {
// should have at least two lines
if state.line + 2 > state.line_max { return None; }

// if it's indented more than 3 spaces, it should be a code block
if state.line_indent(state.line) >= 4 { return None; }
if state.line_indent(state.line) >= state.md.max_indent { return None; }

let next_line = state.line + 1;
if state.line_indent(next_line) < 0 { return None; }

// if it's indented more than 3 spaces, it should be a code block
if state.line_indent(next_line) >= 4 { return None; }
if state.line_indent(next_line) >= state.md.max_indent { return None; }

let alignments = Self::scan_alignment_row(state.get_line(next_line))?;
let header_row = Self::scan_row(state.get_line(state.line));
Expand Down Expand Up @@ -345,8 +343,7 @@ impl BlockRule for TableScanner {
//
if state.line_indent(state.line) < 0 { break; }

// if it's indented more than 3 spaces, it should be a code block
if state.line_indent(state.line) >= 4 { break; }
if state.line_indent(state.line) >= state.md.max_indent { break; }

// stop if the line is empty
if state.is_empty(state.line) { break; }
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/html/html_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ pub struct HtmlBlockScanner;

impl HtmlBlockScanner {
fn get_sequence(state: &mut BlockState) -> Option<&'static HTMLSequence> {
// if it's indented more than 3 spaces, it should be a code block
if state.line_indent(state.line) >= 4 { return None; }

if state.line_indent(state.line) >= state.md.max_indent { return None; }

let line_text = state.get_line(state.line);
let Some('<') = line_text.chars().next() else { return None; };
Expand Down
12 changes: 12 additions & 0 deletions tests/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ fn no_plugins() {
assert_eq!(result, "hello\nworld\n");
}

#[test]
fn no_max_indent() {
let md = &mut markdown_it::MarkdownIt::new();
markdown_it::plugins::cmark::block::paragraph::add(md);
markdown_it::plugins::cmark::block::list::add(md);
md.max_indent = i32::MAX;
let node = md.parse(" paragraph\n - item");
let result = node.render();
assert_eq!(result, "<p>paragraph</p>\n<ul>\n<li>item</li>\n</ul>\n");
}


/*#[test]
fn no_block_parser() {
let md = &mut markdown_it::MarkdownIt::new();
Expand Down

0 comments on commit 78914cd

Please sign in to comment.