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

feat: support prism langs dependencies import validation #2489

Merged
merged 5 commits into from
Sep 16, 2024

Conversation

Koooooo-7
Copy link
Member

@Koooooo-7 Koooooo-7 commented Aug 15, 2024

Summary

The Prism code highlight requires a strict order per langs.
It is hard for users to find out the right order to put the langs importing <script>.
As per the Prism for highlight requires the strict dependencies import order for languages. When user add multi highlight support for much langs, it may put in wrong order.

Why provides the validation instead of the out of box solution:

  • The Prism provide an autoloader plugin to make it possible to auto load langs dependencies instead of user customize it.

    Ideally, user can get the code highlight function directly.
    But it doesn't work very well especially --- remains lots of issues waiting fix PrismJS/autoloader and the PrismJS is not active maintained anymore.
    If we used the autoloader solution, we need facing the same or more issues also.
    And the configs provided from autoloader plugin is too less to do extension.

  • The markedjs or other code highlight libs, such as marked-highlight
    They can provided as extension for marked or another way to highlight code.
    But it will get lots of code styling changed and not support so much langs as PrismJS.
    It is not worthy to refactor the whole code highlight solution.

  • The limited situation for the issue happens.
    The issue occurs when user imports lots of langs with need lots of specific dependencies orders.
    Or some user import a langs but don't know it needs some extra lang dependencies, such as tsx.
    There is no strong voice about it when users use docsify.


Therefore, I think the dependencies validation with warning and the extra docs for Prism in the Prism section should be enough to resolve the unusually importing issue.

TODO: The doc update will finish later after the new callout labels done.


Screenshots

All is correct:
Allfine

Missing import or in wrong order:

   // not import java
 <!-- <script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-java.min.js"></script> -->

   // wrong import order, ruby should first
    <script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-crystal.min.js"></script>
    <script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-ruby.min.js"></script>

     // missing dependencies
    <!-- <script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-jsx.min.js"></script> -->
    <script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-typescript.min.js"></script>
    <script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-tsx.min.js"></script>

Allwrong

Related issue, if any:

close #1952

What kind of change does this PR introduce?

Other

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed

Does this PR introduce a breaking change?

Yes
No

Tested in the following browsers:

  • Chrome
  • Firefox
  • Safari
  • Edge

As per the Prism for highlight requires the strict dependencies import order for languages.
When user add multi highlight support for much langs, it may put in wrong order.
A validation to make user aware the order for each langs' dependencies.
Copy link

vercel bot commented Aug 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docsify-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 12, 2024 2:49am

@trusktr
Copy link
Member

trusktr commented Aug 16, 2024

Hey @Koooooo-7 thanks for taking time to tackle this issue. I am wondering if it is worth adding code to support basically a legacy and unmaintained project, and if it would be better to pick a newer project that is written with ES Modules, because then the order of dependencies is of no worry.

I have been using codemirror. Examples on Lume site:

Those examples are fully editable and show the live output, but the text editor can also be used readonly to display text only.

For the live code samples on Lume site, I created a <live-code> element:

Here is the <code-mirror> element I made for easy usage of codemirror, which is what <live-code> uses for the text portion, it provides only the codemirror text rendering and editing:

The demos there are editable, but it can be easily set to readonly for display. Also, similar to what I did in <live-code>, it is possible to add buttons for copy, reset (if code is editable), etc.

Anyway just sharing for ideas. Maybe there's something simpler, but that is published as ES Modules, that we could use. In any case, I believe it would be ideal to move forward with libs that are easy to support without having to write code like in this PR because ES Modules provide it for free already.

@Koooooo-7
Copy link
Member Author

Hi @trusktr , thx for the sharing.
Cos the prism is more focus on the highlight code and it already supports tons of langs, and docsify uses it about over 7 years. For the highlight requirement, I think it could do well in the job, except the annoying dependencies order.

About the live-code, I think it is pretty cool tho.
The highlight code is just a part of features of it.
I suppose the live-code could be a plugin or we officially support it in docsify like the mermaid .

cc @docsifyjs/reviewers

@trusktr
Copy link
Member

trusktr commented Aug 24, 2024

Btw, check out https://shiki.style/, a more modern syntax highligher than Prism, uses ES modules, etc.

@Koooooo-7
Copy link
Member Author

Koooooo-7 commented Aug 26, 2024

Btw, check out https://shiki.style/, a more modern syntax highligher than Prism, uses ES modules, etc.

The 式 lib looks pretty cool and it does the highlight from the HAST which is more deeper layer to calculate DOMs.
Decide to change the Prism or not, I suppose we could have a discussion in chats before we just jump to do it.

Btw, I'm interesting in making something on it also, its transformer looks fun, hah.

@sy-records sy-records changed the title update: support prism langs dependencies import validation feat: support prism langs dependencies import validation Sep 11, 2024
* and maintains the final global loading status (dummy.loaded) for current lang.
*/
const dummy = {
cur: '',
Copy link
Member

Choose a reason for hiding this comment

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

cur It would be better if abbreviations were not used.

@Koooooo-7 Koooooo-7 merged commit 87e43f1 into develop Sep 16, 2024
9 checks passed
@Koooooo-7 Koooooo-7 deleted the polish-prism branch September 16, 2024 02:42
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.

Language syntax highlight (PrismJS) : language dependency order
3 participants