GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Preserve information about leading whitespace in paragraphs · Issue #301 · commonmark/commonmark-java

Preserve information about leading whitespace in paragraphs #301

New issue
Copy link
New issue
Copy link
Open
Open
Preserve information about leading whitespace in paragraphs#301
Copy link
Labels
enhancementhelp wanted

Description

@panpeter
panpeter
opened on Oct 20, 2023
Issue body actions

When using DocumentParser with no enabled block types, the Text nodes do not include whitespaces at the beginning of a line.

For example, when we use a parser with no enabled block types and the input is:

- text 1
  - text 2

the expected result is a document containing two Text nodes:

  • Text("- text 1")
  • Text(" - text 2")

but the second Text is "- text 2" (without preceding whitespaces)

To better illustrate here is a sample test that fails:

public class ParserTest {
    
    ...
    
    @Test
    public void noBlockTypes() {
        String given = "- text 1\n  - text 2";
        Parser parser = Parser.builder().enabledBlockTypes(Collections.<Class<? extends Block>>emptySet()).build();
        Node document = parser.parse(given);

        Node child = document.getFirstChild();
        assertThat(child, instanceOf(Paragraph.class));

        child = child.getFirstChild();
        assertThat(child, instanceOf(Text.class));
        assertEquals("- text 1", ((Text) child).getLiteral());

        child = child.getNext();
        assertThat(child, instanceOf(SoftLineBreak.class));

        child = child.getNext();
        assertThat(child, instanceOf(Text.class));
        assertEquals("  - text 2", ((Text) child).getLiteral());
    }
}

Activity

  1. panpeter
    added
    bug
    on Oct 20, 2023
  2. robinst commented on Feb 8, 2024

    @robinst
    robinst
    on Feb 8, 2024
    Collaborator
    More actions

    The reason for this is the paragraph parser. The spec says that leading whitespace is skipped: https://spec.commonmark.org/0.31.2/#example-222

    Not sure how we would handle it. We can't add the leading whitespace to the literal of Text nodes as that would change rendering for existing code, but maybe we could add it as another attribute.

    Note that you should be able to work around this limitation by checking the source spans of the text (see includeSourceSpans on Parser.Builder).

  3. robinst commented on Mar 9, 2024

    @robinst
    robinst
    on Mar 9, 2024
    Collaborator
    More actions

    See also #290 (comment)

  4. robinst
    changed the title [-]DocumentParser drops whitespaces at the beginning of a line[/-] [+]Preserve information about leading whitespace in paragraphs[/+] on Mar 23, 2025
  5. robinst
    added
    enhancement
    help wanted
    and removed
    bug
    on Mar 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.