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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unleash the power of Block Directive #23

Merged
merged 2 commits into from Aug 6, 2023

Conversation

LiYanan2004
Copy link
Owner

Modifications

  • Unleash the full power for users to customize their own view instead of using MarkdownView
  • Update APIs
  • Re-structure related files

Custom View inside MarkdownView

Now, BlockDirectiveDisplayable gives you text from the body of block directives.

You can add custom view inside MarkdownView by implementing BlockDirectiveDisplayable.

Example

If you want to support LateX in your app along with MarkdownView.

Define your provider:

import LaTeXSwiftUI // https://github.com/colinc86/LaTeXSwiftUI

struct LatexProvider: BlockDirectiveDisplayable {
    func makeView(arguments: [BlockDirectiveArgument], text: String) -> some View {
        LaTeX(text).id(text)
    }
}

extension BlockDirectiveDisplayable where Self == LatexProvider {
    static var latex: LatexProvider { .init() }
}

Then, adding it to MarkdownView:

MarkdownView(...)
    .blockDirectiveProvider(.latex, for: "latex")

馃帀 That鈥檚 it. Now if you write markdown like this:

# LateX

@latex { Euler's identity is $e^{i\\pi}+1=0$. }

You can see this:

latex

Deprecations

The required function makeView(arguments:innerView:) has been replaced with makeView(arguments:text:)

Tips: If your custom view depends on previous innerView, you can replace it with MarkdownView(text: text)

- Unleash the full power for users to customize their own view instead of using MarkdownView
- Update APIs
- Re-structure related files
@LiYanan2004
Copy link
Owner Author

This enhancement may satisfy the feature request: #19

@LiYanan2004 LiYanan2004 linked an issue Aug 5, 2023 that may be closed by this pull request
@hugo53
Copy link

hugo53 commented Aug 5, 2023

@LiYanan2004 Thank you for your always great work. It works fine on my side. However, it seems @latex is a must in input string. Could you please share how can we go without that annotation? I think in most of Latex texts, there is no @latex annotation.

@LiYanan2004
Copy link
Owner Author

LiYanan2004 commented Aug 5, 2023

@hugo53 You鈥檙e right.

I mean you can now add LateXView as a part of MarkdownView. If you鈥檙e working on views with static markdown text, you can simply update input string to use that pattern. If you are developing document editors, you can try to manually detect this syntax(use the dependency of LateXSwiftUI package) and dynamically wrap it using @latex annotation.

To sum up, It鈥檚 full of potential 馃榿

@hugo53
Copy link

hugo53 commented Aug 5, 2023

Yes I'm thinking about that as we may need to do preprocessing text in that case.
I have found another case, although it is rare, where LaTeX does not work if the text has both a code block and LaTeX text. Please check below image.
Screenshot 2023-08-05 at 15 38 41

@LiYanan2004
Copy link
Owner Author

@hugo53 Congratulation. You found a bug of swift-markdown.馃ぃ This bug only occurs when the code block is the first Markdown item. I will report this bug.

@hugo53
Copy link

hugo53 commented Aug 5, 2023

@LiYanan2004 Ah correct, I added some words at first and it works fine now. Weird bug 馃ぃ

@LiYanan2004
Copy link
Owner Author

@hugo53 Based on the documentation, you can extract ranges that contains LateX Syntax using this method:

func findLateXRanges(in text: String) -> [Range<String.Index>] {
    let singleDollarPattern = #"\$((?!\$).)+\$"#
    let doubleDollarPattern = #"\$\$(.+)\$\$"#
    let squareBracketsPattern = #"\\\[(.+)\\\]"#
    let beginEndPattern = #"\\begin{equation}(.+)\\end{equation}"#
    let beginEndWithAsteriskPattern = #"\\begin{equation*}(.+)\\end{equation*}"#
    let pattern = try! Regex("\(doubleDollarPattern)|\(singleDollarPattern)|\(squareBracketsPattern)|\(beginEndPattern)|\(beginEndWithAsteriskPattern)")
    return text.matches(of: pattern).map { $0.range }
}

@hugo53
Copy link

hugo53 commented Aug 5, 2023

@LiYanan2004 Thank you for suggestion. I will check more on that!

@LiYanan2004 LiYanan2004 merged commit abd3c30 into main Aug 6, 2023
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

Successfully merging this pull request may close these issues.

Latex support
2 participants