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

fix: handling empty line break doc at the end. #1776

Merged
merged 13 commits into from
Jul 10, 2024
Merged

fix: handling empty line break doc at the end. #1776

merged 13 commits into from
Jul 10, 2024

Conversation

a-rena
Copy link
Contributor

@a-rena a-rena commented Jun 27, 2024

Issue: When rendering document to markdown, if the document ends with empty line break on new line, its returning an additional "" at the of the document.

Fix: Added additional condition on line break Generator to check specific condition.

@a-rena
Copy link
Contributor Author

a-rena commented Jun 27, 2024

@copilot-robot prerelease

@copilot-robot
Copy link

Hi, @a-rena, a pre-release has been published:

  • @atjson/renderer-commonmark@0.30.5-COPILOT-12007.2+7e64ee2b

bachbui and others added 2 commits June 27, 2024 11:05
Our markdown syntax does not support teminal line-breaks, either
in blocks like a paragraph or just in text.

We had correctly been suppressing rendering the slash-escaped line
break when it appeared in a paragraph, but not in text
@a-rena
Copy link
Contributor Author

a-rena commented Jun 28, 2024

@copilot-robot prerelease

@copilot-robot
Copy link

Hi, @a-rena, a pre-release has been published:

  • @atjson/renderer-commonmark@0.30.5-COPILOT-12007.7+46f34de4

@a-rena
Copy link
Contributor Author

a-rena commented Jul 3, 2024

@copilot-robot prerelease

@copilot-robot
Copy link

Hi, @a-rena, a pre-release has been published:

  • @atjson/renderer-commonmark@0.30.5-COPILOT-12007.11+dc124610

g changes
@a-rena
Copy link
Contributor Author

a-rena commented Jul 3, 2024

@copilot-robot prerelease

@copilot-robot
Copy link

Hi, @a-rena, a pre-release has been published:

  • @atjson/renderer-commonmark@0.30.5-COPILOT-12007.12+e0275c68

@a-rena
Copy link
Contributor Author

a-rena commented Jul 3, 2024

@copilot-robot prerelease

@copilot-robot
Copy link

Hi, @a-rena, a pre-release has been published:

  • @atjson/renderer-commonmark@0.30.5-COPILOT-12007.13+57716cb3

Copy link
Contributor

@bachbui bachbui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lgtm, but I encourage you to get a second opinion since I coded some of this myself

@a-rena a-rena requested a review from tim-evans July 4, 2024 05:38
Copy link
Collaborator

@tim-evans tim-evans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bachbui is there any reason we're not traversing the end of the document in the LineBreak hook to stop those from rendering or render the alternate syntax for line breaks?

@bachbui
Copy link
Contributor

bachbui commented Jul 8, 2024

@bachbui is there any reason we're not traversing the end of the document in the LineBreak hook to stop those from rendering or render the alternate syntax for line breaks?

@tim-evans I'm not sure what you mean by traversing the end of the document but this does include a check in the Linebreak renderer here https://github.com/CondeNast/atjson/pull/1776/files#diff-8daeaec5afbb513a29fdfe85dd49e2eb3a6bcaeb157303818f5f88f66abf723eR481 where we check the context. However, this check only checks if there is a next sibling which is insufficient and would fail to account for cases we have in our tests like:

  • repeated linebreaks at the end of a document
  • linebreaks interspersed with newline characters in the text
  • linebreaks inside of a mark

Accounting for these cases in the rendering context would be a bit complicated (and I think would have to be specifically for each of the cases) compared to the current approach of rendering somewhat naively and just verifying we don't have this specific markdown pattern. But perhaps I am missing a better way to do this - open to suggestions!

@tim-evans
Copy link
Collaborator

@bachbui yea, I think there may be some alternate ways to do this. One thing I would recommend is to switch the test cases not to use the serialized atjson format for testing.

@tim-evans
Copy link
Collaborator

@bachbui I think making a preProcess hook in the renderer hir base class that will allow us to remove line breaks from the peritext document prior to rendering would make the most sense. That way we don't have to deal with lots of context jumping / etc and can handle it in a single pass.

@tim-evans
Copy link
Collaborator

This hook kind of works for most cases, but strips some markdown that we want to keep for commonmark compatibility:

protected preProcess(doc: { text: string; marks: Mark[]; blocks: Block[] }) {
    // Line breaks cannot end markdown block elements or paragraphs
    // https://spec.commonmark.org/0.29/#example-641
    let matches = doc.text.match(/[\uFFFC\s]+$/g);
    if (matches) {
      let text = matches[0];
      let i = 0;
      let position = text.lastIndexOf("\uFFFC");
      if (position !== -1) {
        while (position !== -1) {
          let block = doc.blocks[doc.blocks.length - i - 1];
          if (block.type !== "line-break" && block.type !== "paragraph") {
            break;
          }
          i++;
          position = text.lastIndexOf("\uFFFC", position) - 1;
        }

        return {
          text: doc.text.slice(
            0,
            doc.text.length - (text.length - position - 1)
          ),
          blocks: doc.blocks.slice(0, doc.blocks.length - i),
          marks: doc.marks,
        };
      }
    }

    return doc;
  }

@tim-evans
Copy link
Collaborator

In leiu of preprocessing, I may recommend changing the markdown syntax for line breaks to the 2-space + newline to solve issues of newlines at the end of text. This should be identical to the backslash + newline sequence but not have any side effects that are visible on sites

Instead of suppressing the rendering of linebreaks when they
terminate a block/document, we can change our linebreak md syntax
to an alternative that doesn't break the parser.

The alternative linebreak sequence is `  \n` instead of `\\\n`.
When this appears at the end of a document, it is still parsed
as a linebreak, so it is fine to always render it.
@bachbui
Copy link
Contributor

bachbui commented Jul 9, 2024

What a great suggestion. We can determine from our toggle and round trip tests if this produces any issues

@bachbui
Copy link
Contributor

bachbui commented Jul 9, 2024

@copilot-robot prerelease

@copilot-robot
Copy link

Hi, @bachbui, a pre-release has been published:

  • @atjson/renderer-commonmark@0.30.5-COPILOT-12007.17+58285767

@a-rena a-rena merged commit e318ce5 into main Jul 10, 2024
6 checks passed
@a-rena a-rena deleted the COPILOT-12007 branch July 10, 2024 10:55
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

Successfully merging this pull request may close these issues.

4 participants