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

Unable to markdown text if there are indents #143

Open
HassanTaleb90 opened this issue Sep 2, 2023 · 5 comments
Open

Unable to markdown text if there are indents #143

HassanTaleb90 opened this issue Sep 2, 2023 · 5 comments

Comments

@HassanTaleb90
Copy link

Unable to markdown text if there is a static indents before each line

//Bug

let str = """
	Creates a personalized greeting for a recipient.

	- Parameter recipient: The person being greeted.

	- Throws: `MyError.invalidRecipient` if `recipient` is "Derek" (he knows what he did).

	- Returns: A new string saying hello to `recipient`.
"""

//No problem

let str = """
 Creates a personalized greeting for a recipient.

 - Parameter recipient: The person being greeted.

 - Throws: `MyError.invalidRecipient` if `recipient` is "Derek" (he knows what he did).

 - Returns: A new string saying hello to `recipient`.
 """
@HassanTaleb90 HassanTaleb90 changed the title Unable to markdown text if there is indents Unable to markdown text if there are indents Sep 2, 2023
@georgemp
Copy link
Contributor

I just did a quick test. The first example seems to be parsed as an indented code block - which is part of the spec, while the latter is parsed as a list. If you'd like both to be parsed as lists, then I believe the number of spaces in the first example should be less than four.

@QuietMisdreavus
Copy link
Contributor

This is a result of Swift's syntax for multi-line string literals - the "beginning of the line" is determined by how indented the closing """ is. By having it so far over from the content, you're telling the Swift compiler to add all that leading indentation to the resulting string. If you want to have the string contents indented like the first sample, you need to also indent the closing delimiter:

let str = """
	Creates a personalized greeting for a recipient.

	- Parameter recipient: The person being greeted.

	- Throws: `MyError.invalidRecipient` if `recipient` is "Derek" (he knows what he did).

	- Returns: A new string saying hello to `recipient`.
	"""

@HassanTaleb90
Copy link
Author

What I do:

  1. I use SwiftSyntax to get docBlockComment text.
  2. This text contains the leading indents.
  3. I use let document = Document(parsing: netCommentStr) to visit it and apply styling.
  4. netCommentStr is the docBlockComment text after trimming /** and */

The main problem is that netCommentStr contains the leading indents.
Looks like I have to trim these indents before using it in Document parsing.

I would prefer that this problem be dealt automatically by ignoring leading indents.

Screenshot 2023-09-18 at 10 42 43 PM

@QuietMisdreavus
Copy link
Contributor

Indented code blocks are a feature of basic Markdown; automatically stripping leading indentation would remove a foundational feature of Markdown syntax. This seems to be an issue in SwiftSyntax; all the reference documentation i could find about multi-line comments just says that the comment is the content between the /** and */ delimiters, without any reference to the indentation. I believe the compiler automatically strips this indentation before generating the symbol graph that is loaded by Swift-DocC, so i would expect SwiftSyntax to provide a similar mechanism.

@HassanTaleb90
Copy link
Author

I think we could exclude the indents according to leading indents in first line.

Example: If there is two tabs as leading indents in first line, we check if there is the same in each next line to exclude them all.

I beleave it is a "markdown" issue, because if there are repeated leading indents in each line, we just need to ignore them.
After that, the remaining leading indents are of cource useful.

Screenshot 2023-09-19 at 7 45 21 AM

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

No branches or pull requests

3 participants