facebook / lexical-ios

Lexical iOS is an extensible text editor framework that integrates the APIs and philosophies from Lexical Web with a Swift API built on top of TextKit.
MIT License
549 stars 41 forks source link

LexicalMarkdownPlugin: say hello to markdown exports #50

Closed mansimransingh closed 1 year ago

mansimransingh commented 1 year ago

This is a first pass at implementing Markdown support for lexical.

It depends on Apple's swift-markdown package.

It needs tests and covering more edges cases (embedded lists, indentations, etc) - but is enough to get started with.

I am also working on a markdown import for our use case, which I will add to this plugin in the next day or so.

mansimransingh commented 1 year ago

Swift markdown supports a whole set of formatting options which make it easy to customise the output:

https://apple.github.io/swift-markdown/documentation/markdown/markupformatter/options/

The documentation is nice, but if you don't want to click through multiple pages, here is the source where its implemented.

If it doesn't support your exact variant, it should be relatively straight forward to have a fork.

facebook-github-bot commented 1 year ago

@amyworrall has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

mansimransingh commented 1 year ago

@amyworrall im yet to fix the indentation issues. Everything else is addressed.

for my project, I went another route. The javascript library has markdown import/export support and we can rely on the fact that JSON internals exported from JS are compatible with swift library (besides this fix https://github.com/facebook/lexical-ios/pull/51).

So i packed the javascript library along with 2 utility functions (convert markdown -> JSON and JSON -> markdown), and using the Javascript headless lexical editor, over a JSContext - i can do the format conversions. This turned out to be quicker than implementing the markdown import reliably.

If you're interested, i can create a branch for that too.

facebook-github-bot commented 1 year ago

@amyworrall has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

amyworrall commented 1 year ago

So i packed the javascript library along with 2 utility functions (convert markdown -> JSON and JSON -> markdown), and using the Javascript headless lexical editor, over a JSContext - i can do the format conversions. This turned out to be quicker than implementing the markdown import reliably.

If you're interested, i can create a branch for that too.

That sounds interesting from a tech perspective! For a long term Lexical iOS perspective, we probably do want to do things natively.

Did you make an attempt at the native Markdown import? If so, would you mind sharing what issues you ran into? That may save me some time when I attempt to look at it :)

facebook-github-bot commented 1 year ago

@amyworrall has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

facebook-github-bot commented 1 year ago

@amyworrall has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

mansimransingh commented 1 year ago

I added a live markdown view in the playground and started work on imports on a branch here:

https://github.com/mansimransingh/lexical-ios/tree/markdown-importing

Just have to go through each MarkupVisitor function and implement correctly / map it to a lexical node.

It stacks on top of this PR.

facebook-github-bot commented 1 year ago

@amyworrall has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

amyworrall commented 1 year ago

I added a live markdown view in the playground and started work on imports on a branch here:

Awesome, will check it out.

BTW sorry for the churn on the "imported this pull request" messages -- having some trouble wiht our internal tooling.

facebook-github-bot commented 1 year ago

@amyworrall merged this pull request in facebook/lexical-ios@cdb01947e2ea20f4e690a872a10ebf76ba1f803d.

amyworrall commented 1 year ago

hi! Have merged. Had to change the name of the SwiftMarkdown library, we call it SwiftMarkdown internally not just Markdown. Haven't checked that still works with the playground, will check tomorrow.