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

Improve line splitting when linebreak is forced #326

Open
mdeweerd opened this issue May 28, 2022 · 4 comments
Open

Improve line splitting when linebreak is forced #326

mdeweerd opened this issue May 28, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@mdeweerd
Copy link

Context

The line split is not convienient when forcing a line break when running

 mdformat --wrap 79 test.md

where test.md is

1.  Lorem ipsum dolor sit amet, consectetur adipiscing\
    elit. Aliquam nec odio bibendum lacus auctor auctor in non purus. Morbi dapibus mauris orci, a sollicitudin eros dapibus at. Suspendisse potenti. Phasellus tempor scelerisque nibh, vel condimentum felis sollicitudin vitae. Mauris consectetur ex quis elit laoreet congue. Proin rutrum turpis et efficitur dignissim. Sed aliquam sapien vitae rhoncus convallis. Nullam dictum erat suscipit justo eleifend tincidunt. Morbi mauris lectus, ullamcorper in accumsan non, commodo a sem.

because it becomes:

1. Lorem ipsum dolor sit amet, consectetur adipiscing\
   elit. Aliquam nec odio
   bibendum lacus auctor auctor in non purus. Morbi dapibus mauris orci, a
   sollicitudin eros dapibus at. Suspendisse potenti. Phasellus tempor
   scelerisque nibh, vel condimentum felis sollicitudin vitae. Mauris
   consectetur ex quis elit laoreet congue. Proin rutrum turpis et efficitur
   dignissim. Sed aliquam sapien vitae rhoncus convallis. Nullam dictum erat
   suscipit justo eleifend tincidunt. Morbi mauris lectus, ullamcorper in
   accumsan non, commodo a sem.

The new line after the forced line break seems to continue the line length count using the previous line.

Version info:

mdformat 0.7.13 (mdformat_frontmatter: 0.4.1, mdformat_gfm: 0.3.5, mdformat_tables: 0.4.1,
mdformat_toc: 0.3.0)

Proposal

No response

Tasks and updates

No response

@mdeweerd mdeweerd added the enhancement New feature or request label May 28, 2022
@hukkin hukkin added bug Something isn't working and removed enhancement New feature or request labels May 30, 2022
@hukkin
Copy link
Member

hukkin commented May 30, 2022

Thanks for the issue! A PR is welcome to fix this.

@mdeweerd
Copy link
Author

mdeweerd commented May 30, 2022

I hear you, but it"s not that obvious where the length of a line is managed.

@lofidevops
Copy link

This hack fixes test_wrap__hard_break which is currently skipped. I suspect a better solution is possible.

def paragraph(node: RenderTreeNode, context: RenderContext) -> str:  # noqa: C901
    inline_node = node.children[0]
    text = inline_node.render(context)

    lines = []
    for line in text.split("\n"):
        lines.append(_paragraph_line(line, context))

    return "\n".join(lines)


def _paragraph_line(text: str, context: RenderContext) -> str:

    if context.do_wrap:
        wrap_mode = context.options["mdformat"]["wrap"]
        if isinstance(wrap_mode, int):
            wrap_mode -= context.env["indent_width"]
            wrap_mode = max(1, wrap_mode)
        text = _wrap(text, width=wrap_mode)
    ...

@lofidevops
Copy link

This could be caused by bad detection in makdown-it-py, see executablebooks/markdown-it-py#172 for an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants