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

How to preserve empty lines? #705

Closed
Jinjinov opened this issue Mar 3, 2023 · 5 comments
Closed

How to preserve empty lines? #705

Jinjinov opened this issue Mar 3, 2023 · 5 comments
Labels

Comments

@Jinjinov
Copy link

Jinjinov commented Mar 3, 2023

Would it be possible to preserve empty lines?

Using UseSoftlineBreakAsHardlineBreak() preserves new lines, but not empty lines.

I tried using text.Replace("\n", "<br>") but then titles and lists don't work.

I tried using text.Replace("\n", "<br>\n") then titles and lists work, but there are too many empty lines.

I tried using text.Replace("\n\n", "<br><br>") but then all markdown after a list is wrong.

I tried using text.Replace("\n\n", "<br>\n") then titles and lists work, but not all empty lines are preserved.

I tried using text.Replace("\n\n", "<br><br>\n") then titles and lists work, but everything else is wrong.

Would it be possible to specify my own rule for this?

@xoofx xoofx added the question label Mar 3, 2023
@xoofx
Copy link
Owner

xoofx commented Mar 3, 2023

Probably duplicate of #698.

Would it be possible to preserve empty lines?
Would it be possible to specify my own rule for this?

The CommonMark specs are treating empty lines in a specific way (e.g they are used to stop processing list..etc.) and they have many associated rules with them.

Changing this behavior (even with an opt-in switch) is not something I would like to maintain, and I'm not even sure it is practical, feasible without conflicting with existing rules. As you realized yourself, empty lines have a context dependent behavior.

So my main advice would be to not pursue this road 🙂

@Jinjinov
Copy link
Author

Jinjinov commented Mar 3, 2023

I understand that this is not compliant with CommonMark and would make no sense in implementing it in Markdig.

I am writing a note editor with these specifications:

  1. all url links must be converted from simple text to <a href>
  2. all empty lines must be preserved
  3. using Markdown for titles, lists, preformatted text is a bonus (optional)

I decided to use Markdig, because it covers 1. and 3. but the most important features are detecting links in simple text and preserving empty lines.

Can you give me a few starting points how could I write a custom Markdig extension that would preserve empty lines?

@Jinjinov
Copy link
Author

Jinjinov commented Mar 3, 2023

This is what ChatGPT wrote, but it doesn't work:

public class PreserveEmptyLinesExtension : IMarkdownExtension
{
    public void Setup(MarkdownPipelineBuilder pipeline)
    {
        // Add a block parser that preserves empty lines
        pipeline.BlockParsers.Insert(0, new PreserveEmptyLinesParser());
    }

    public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
    {
        // No renderer modifications necessary
    }

    private class PreserveEmptyLinesParser : BlockParser
    {
        public PreserveEmptyLinesParser()
        {
            OpeningCharacters = new char[0];
        }

        public override BlockState TryOpen(BlockProcessor processor)
        {
            // Preserve the empty line as a new line block
            if (processor.IsBlankLine)
            {
                processor.NewBlocks.Push(new ParagraphBlock(this));
                return BlockState.Continue;
            }

            return BlockState.None;
        }

        public override BlockState TryContinue(BlockProcessor processor, Block block)
        {
            // Continue the block normally
            return BlockState.None;
        }
    }
}

@xoofx
Copy link
Owner

xoofx commented Mar 3, 2023

Can you give me a few starting points how could I write a custom Markdig extension that would preserve empty lines?

This more likely not trivial and unlikely possible by extending Markdig. You would have to fork it and modify its internals.

@Jinjinov
Copy link
Author

Jinjinov commented Mar 3, 2023

Thank you for your explanations.

@Jinjinov Jinjinov closed this as completed Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants