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

Hard breaks aren't reflected in output #477

Closed
2 tasks done
amartini51 opened this issue Oct 28, 2022 · 10 comments · Fixed by #633 or apple/swift-docc-render#714
Closed
2 tasks done

Hard breaks aren't reflected in output #477

amartini51 opened this issue Oct 28, 2022 · 10 comments · Fixed by #633 or apple/swift-docc-render#714
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@amartini51
Copy link

amartini51 commented Oct 28, 2022

Description

The CommonMark spec allows writers to specify a line break (not a paragraph break) by writing two or more spaces at the end of the line, or by writing a backslash (\) at the end of the line. Currently, the rendered content from DocC doesn't break the line at that location.

Example input:

This text should\
appear on two lines.

This is the second paragraph.

Checklist

  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue.

rdar://101684224

@amartini51 amartini51 added the bug Something isn't working label Oct 28, 2022
@ethan-kusters ethan-kusters added the good first issue Good for newcomers label Oct 28, 2022
@ethan-kusters
Copy link
Contributor

Thanks for filing this @amartini51!

CC: @QuietMisdreavus

It looks like this fix should be in RenderContentCompiler here:

mutating func visitLineBreak(_ lineBreak: LineBreak) -> [RenderContent] {
return [RenderInlineContent.text(" ")]
}
.

amartini51 referenced this issue in apple/swift-book Nov 4, 2022
DocC doesn't support subscripts, so instead of using subscript "opt" for
optionality, use a postfix question mark. Alternatives considered
included text like "OPT" or "(opt)" and Unicode subscript characters.
The former are hard to read and interrupt the flow of the grammar. The
latter render poorly because they're intended for phonetic annotation,
so the different letters don't share a baseline in many fonts.

It's a known issue that DocC doesn't support hard breaks [1], so insert a
paragraph break between each grammar production for now. I added a
"double" paragraph break between groups, where there used to be a blank
line, to preserve that information for when DocC adds hard break support
in the future.

1: https://github.com/apple/swift-docc/issues/412

Fixes #3

Fixes rdar://101001280
@Kyle-Ye
Copy link
Collaborator

Kyle-Ye commented Nov 7, 2022

Thanks for filing this @amartini51!

CC: @QuietMisdreavus

It looks like this fix should be in RenderContentCompiler here:

mutating func visitLineBreak(_ lineBreak: LineBreak) -> [RenderContent] {
return [RenderInlineContent.text(" ")]
}

It looks to be a swift-markdown bug for me.

import Markdown
let string = """
This text should\
appear on two lines.

This is the second paragraph.
"""
let document = Document(parsing: string)
print(document.debugDescription())

// Document
// ├─ Paragraph
// │  └─ Text "This text shouldappear on two lines."
// └─ Paragraph
//    └─ Text "This is the second paragraph."

Also even if I add such text to a docc markdown file, and run docc preview on it. The method visitLineBreak does not seem to be called.

@ethan-kusters
Copy link
Contributor

Hm okay. Let's transfer this issue over there for now then. We can file a new issue on Swift-DocC if it turns out a fix is needed across both repos.

@ethan-kusters ethan-kusters transferred this issue from apple/swift-docc Nov 7, 2022
@Iikeli
Copy link

Iikeli commented Jan 27, 2023

Works fine for swift-markdown. Notice that you escape with \ the new line being created in the multiline string. If you wanted to use the backslash, then you need to escape that (with another backslash). See: https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html

let source = """
             Paragraph.\\
             Still the same paragraph.

             Different paragraph
             """
let document = Document(parsing: source)

print(document.debugDescription())

// Document
// ├─ Paragraph
// │  ├─ Text "Paragraph."
// │  ├─ LineBreak
// │  └─ Text "Still the same paragraph."
// └─ Paragraph
// └─ Text "Different paragraph"

@Kyle-Ye
Copy link
Collaborator

Kyle-Ye commented Feb 12, 2023

import Markdown

// Two space at the end of line 4 of string
let string = #"""
This text should\
appear on two lines.

This text should  
appear on two lines.

This is the second paragraph.
"""#
let document = Document(parsing: string)
print(document.debugDescription())
Document
├─ Paragraph
│  ├─ Text "This text should"
│  ├─ LineBreak
│  └─ Text "appear on two lines."
├─ Paragraph
│  ├─ Text "This text should"
│  ├─ LineBreak
│  └─ Text "appear on two lines."
└─ Paragraph
   └─ Text "This is the second paragraph."

As @Iikeli explained, I forgot to escape the "\". So the markdown has no problem identifying linebreak for 2 spaces and "\".

It looks like this fix should be in RenderContentCompiler here:

mutating func visitLineBreak(_ lineBreak: LineBreak) -> [RenderContent] {
return [RenderInlineContent.text(" ")]
}
.

If we change the function to return "\n" for RenderInlineContent

mutating func visitLineBreak(_ lineBreak: LineBreak) -> [RenderContent] {
    return [RenderInlineContent.text("\n)]
}

the output json file in .docc-build/data/documentation/demokit.json is expected as follows

{
    "primaryContentSections": [
        {
            "kind": "content",
            "content": [
                ...
                {
                    "type": "paragraph",
                    "inlineContent": [
                        {
                            "type": "text",
                            "text": "This text should"
                        },
                        {
                            "type": "text",
                            "text": "\n"
                        },
                        {
                            "type": "text",
                            "text": "appear on two lines."
                        }
                    ]
                },
                ...
            ]
        }
    ],
    ...
}

But swift-docc-render (Xcode 14.2 Swift toolchain) will still render the paragraph in one line.

So maybe the issue needs a swift-docc and swift-docc-render cross PR to fix.

Could you help transform the issue back to swift-docc @ethan-kusters ? And when the cross PR is ready, I'll file a new issue on swift-docc-render repo.

@ethan-kusters ethan-kusters transferred this issue from apple/swift-markdown Feb 12, 2023
@gwynne
Copy link
Contributor

gwynne commented Jun 18, 2023

Is there any hope of some motion on this? Still hasn't been fixed as of the Xcode 15 beta/Swift 5.9

@Kyle-Ye
Copy link
Collaborator

Kyle-Ye commented Jun 18, 2023

Is there any hope of some motion on this? Still hasn't been fixed as of the Xcode 15 beta/Swift 5.9

Sorry, I almost forgot the issue 😢. Thanks for the reminding.

@Kyle-Ye
Copy link
Collaborator

Kyle-Ye commented Jun 18, 2023

Is there any hope of some motion on this? Still hasn't been fixed as of the Xcode 15 beta/Swift 5.9

I've added the swift-docc PR via #633 and created the corresponding issue to swift-docc-render via apple/swift-docc-render#697. When both of them are ready, the issue will be closed.

Then we may consider cherry-pick this back to the release/5.9 branch.

@Kyle-Ye
Copy link
Collaborator

Kyle-Ye commented Jul 11, 2023

Is there any hope of some motion on this? Still hasn't been fixed as of the Xcode 15 beta/Swift 5.9

The fix had landed on main branch of swift-docc and swift-docc-render. And we are cherry-picking it back to release/5.9.

@mportiz08
Copy link
Contributor

Both PRs have been merged and also cherry-picked to release/5.9 now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
6 participants