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

Parsing content in a fenced block (for a new plugin) #15

Open
digitalmoksha opened this issue Mar 12, 2023 · 4 comments
Open

Parsing content in a fenced block (for a new plugin) #15

digitalmoksha opened this issue Mar 12, 2023 · 4 comments

Comments

@digitalmoksha
Copy link

Hi, I'm writing a plugin that adds syntax for multiline block quotes, such as

>>>
material to be block quoted
>>>

In general I have it working by creating a version of FenceScanner (as MultilineBlockquoteScanner) and CodeFence (as MultilineBlockquote). Of course, the code fencing doesn't parse the content inside the fence, which needs to happen for a blockquote.

I've replaced the fmt.text(&self.content); with fmt.contents(&node.children); when rendering the new MultilineBlockquote node.

However I'm having trouble figuring out how to actually get the contents parsed and added as children.

Any suggestions?

@digitalmoksha
Copy link
Author

I added

        node.children.push(Node::new(InlineRoot::new(content, mapping)));

and that gets the first line of the block parsed, but not the rest of it.

Looking at https://github.com/rlidwka/markdown-it.rs/blob/master/src/parser/block/builtin/block_parser.rs, I don't see an analogous BlockRoot available.

Or would I need to run the block parser directly?

The code is in https://gitlab.com/digitalmoksha/glfm_multiline_blockquote.rs/-/blob/master/src/lib.rs

@digitalmoksha
Copy link
Author

Ideally a Rust port of https://github.com/markdown-it/markdown-it-container would solve it

@marcuswhybrow
Copy link

I've got things working like this.

use markdown_it::{NodeValue};
use markdown_it::parser::block::{BlockRule, BlockState};
use markdown_it::common::sourcemap::SourcePos;

struct MyBlockType;

impl NodeValue for MyBlockType {...}

struct MyBlockTypeScanner;

impl BlockRule for MyBlockTypeScanner {
    fn run(state: &mut BlockState) -> Option<(Node, usize)> {
        let start_line = state.line;
        let end_line = start_line + 10; // for example
    
        let (content, _mapping) = state.get_lines(start_line, end_line);
        
        let mut node = state.md.parse(content);
        node.replace(MyBlockType);
        node.srcmap = Some(SourcePos::new(start_line, end_line));
        Some((node, end_line - start_line));
    }
}

P.S. I'm just guessing with the source map stuff. Not sure how to test it.

@marcuswhybrow
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants