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 extra empty line issue on tutorial #663

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/utils/syntax-highlight.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2021 Apple Inc. and the Swift project authors
* Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -192,7 +192,7 @@ function duplicateMultilineNode(element) {

// wrap each new line with the current element's class
const result = getLines(element.innerHTML)
.reduce((all, lineText) => `${all}<span class="${className}">${lineText || '\n\n'}</span>\n`, '');
.reduce((all, lineText) => `${all}<span class="${className}">${lineText || ''}</span>\n`, '');
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm. This change likely fixes one bug but it unfortunately re-introduces another that we previously had. These newlines were added to fix a bug where empty lines in a multiline Swift string were accidentally getting stripped.

With this code listing as an example:

let multiline = """
Needs

Spaces

Between

Lines
"""

On main, this gets rendered how it looks there.

On this branch, this gets rendered as follows:

let multiline = """
Needs
Spaces
Between
Lines
"""

Ideally we could find a solution that fixes #662 without re-introducing this other bug, although I know it's tricky because of the complex logic we have to handle line numbers and syntax highlighting.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think #502 was the PR for the bug fix that this change might accidentally re-introduce for context.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. This PR does not re-introduces the bug you mention. It passed the existing test and I tested this form and confirm it locally.

Because the original use nothing. #502 add || "\n\n". But actually add || "" is enough.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately it does re-introduce the issue:

In your docs, of you have this:

let multiline = """
Needs

Spaces

Between

Lines
"""

It will generate this:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately it does re-introduce the issue:

In your docs, of you have this:

let multiline = """
Needs

Spaces

Between

Lines
"""

It will generate this:

image

Got it. I thought it occur on the tutorial side.😂 And it was OK in tutorial steps.


// return a list of newly wrapped HTML elements
return htmlToElements(result.trim());
Expand Down
12 changes: 3 additions & 9 deletions tests/unit/utils/syntax-highlight.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,11 @@ describe("syntax-highlight", () => {
expect(sanitizedCode).toMatchInlineSnapshot(`
<span class="syntax-keyword">let</span> multiline = <span class="syntax-string">""</span><span class="syntax-string">"</span>
<span class="syntax-string">Needs</span>
<span class="syntax-string">

</span>
<span class="syntax-string"></span>
<span class="syntax-string">Spaces</span>
<span class="syntax-string">

</span>
<span class="syntax-string"></span>
<span class="syntax-string">Between</span>
<span class="syntax-string">

</span>
<span class="syntax-string"></span>
<span class="syntax-string">Lines</span>
<span class="syntax-string">"</span><span class="syntax-string">""</span>
`);
Expand Down