-
Notifications
You must be signed in to change notification settings - Fork 242
Add support for custom attributes #25
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
Add support for custom attributes #25
Conversation
|
I'm running in to the same issue (swift-markdown crashing) with my app that is processing markdown containing custom attributes. I noticed that the commit history of swift-cmark is referring to these as inline directives. Instead of calling it |
QuietMisdreavus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @parkera in case you have opinions on naming/serialization in Swift-Markdown, or the use of attributes in Swift generally
This looks good so far! Could you add some tests for the custom attribute type? Something like the ones in Tests/MarkdownTests/Inline Nodes/LinkTests.swift should be enough here.
|
Generally, from the point of view of a generalized Markdown library, i'm fine vending the attribute items as plain text, since that mirrors what the extension in swift-cmark-gfm does. Same for the use of |
|
Thank you for this PR! I think treating these as As for the name, I think it's fine. One thing to keep in mind, perhaps, is that |
|
Circling back to this PR. @christianselig, do you think you'll have time to make some tweaks? If not, let me know and i can push it across the finish line.
One easy way to address this would be to call the type "inline attributes" instead of "custom attributes". A lot of the swift-cmark code just uses the name "attribute" when referring to the node kind, but it's always in the context of inline nodes. Some places use "custom attributes", but i think in the context of parsing them out like this, saying "inline attributes" or just "attributes" should be fine. |
|
@QuietMisdreavus Absolutely can do, I'll get working on this and see to changing "custom attributes" to "inline attributes". |
|
hey guys, any update on this? Would love to see it merged too. Thank you! |
|
@christianselig If you don't have time to come back to this, i can pick it up get it across the finish line. |
|
So sorry, I'll do it this coming week, this has been at the top of my to do list for awhile but the iOS 16 stuff has commanded a bit more of my time than I originally thought (I'm sure you have a lot on your plate too there!) |
|
@QuietMisdreavus Should all be finished now, again very sorry for the delay in completing this.
I also attempted to resolve the merge conflict by adding in the new addition to the MarkupTreeDumper file but it looks like you might need to sign off on that. Let me know if there's anything further I can do and I promise I'll complete it faster this time. :) |
| See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| */ | ||
|
|
||
| /// A set of one or more custom attributes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this comment should be updated, but this file should probably be renamed.
|
GitHub seems to think that there's still a merge conflict? Not sure if something landed between when you pulled |
0bf1200 to
d491147
Compare
|
Okay, I think I did this correctly despite accidentally closing the PR in the process. 😛 File (and comment) should be renamed properly, and I pulled down the Apple repo fresh so there should be no further merge conflicts. |
|
@swift-ci Please test |
QuietMisdreavus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, thanks so much! I double-checked this branch against Swift-DocC and it looks like this doesn't break anything over there, so i'll go ahead and land this. (For the moment docc will skip printing anything for inline attributes it sees, but it's a far cry from crashing! Proper support will require some work on Swift-DocC to determine what we want to do with it.)
QuietMisdreavus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, sorry for the back and forth, but could you add some tests? You could drop a single function in a new file Tests/MarkdownTests/Inline Nodes/InlineAttributeTests.swift and base it off some of the other tests there, like in SymbolLinkTests how it parses a Document and checks its debug dump.
|
No problem! How'd I do? |
|
Looks good, but i was also hoping to see something that parsed a Document, just because that's the crashing path in Swift-DocC. 😅 Something like this... func testParseInlineAttributes() {
let source = "^[Hello, world!](rainbow: 'extreme')"
let document = Document(parsing: source)
let expectedDump = """
Document @1:1-1:37
└─ Paragraph @1:1-1:37
└─ InlineAttributes @1:1-1:37 attributes: `rainbow: 'extreme'`
└─ Text @1:3-1:16 "Hello, world!"
"""
XCTAssertEqual(expectedDump, document.debugDescription(options: .printSourceLocations))
} |
|
On it! |
|
Sorry for the delay here, added that Document-based test (thank you for writing that), if there's any more tests or anything you'd like written just say so and I'll be (more quickly!) on it! |
QuietMisdreavus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, thanks so much! Let's land this!
|
@swift-ci Please test |
Currently the library will crash if you input Markdown such as the following:
This is because while the swift-cmark side of things is knowledgeable about custom attributes (also see WWDC21 "What's New in Foundation @ 10:12" for its usage), this library's Swift counterpart to the C code isn't aware of custom attributes, and fatal errors with an unknown type if such an example is encountered. (Ask me how I know!)
This PR adds a
CustomAttributesSwift type into the library as well as some functions to get it to work, thereby preventing the crash and letting users implement custom attributes in their Walkers/Visitors/Rewriters.Some design decisions I wasn't 100% sure of:
CustomAttributes. The plural does seem necessary as the actual custom attributes (per the WWDC video) can be JSON5 with any number of attributes, rather than just 1 in the example above.Customas a prefix is also debatable, butAttributesfelt too generic, andCustomBlockalready exists.CustomAttributesobject should have a value that is aString(the raw string/JSON representing the attributes), or a[String: AnyHashable]dictionary where the values are actually broken down. I erred on the side of a string, as it would allow the user to ingest it more flexibly, through a customCodableimplementation,JSONSerialization, etc. rather trivially, instead of the API trying to guess what they want their output as.MarkupFormattercould technically have each key-value pair on a separate line to better adhere topreferredLineLimit, however formatting the output as such also gets into the weeds of JSON formatting, proper indentation, and things likely beyond the scope of the formatter so I elected not to.(Thank you to @QuietMisdreavus for help.)