Giter VIP home page Giter VIP logo

Comments (11)

jussivirtanen avatar jussivirtanen commented on August 25, 2024 5

It's true that GraphCMS doesn't natively support selecting a language for code blocks within a rich text field. However, what you can do is to can create a new model on GraphCMS for your code blocks and add language as a text field – or any other data you might want to show with your code snippets, like a filename. Then you can query the model within your rich text and get everything you need. Something like this:

CleanShot 2022-02-08 at 09 55 55

Then you need to enable the model to be embedded in your rich text field:

CleanShot 2022-02-08 at 09 57 59

And finally, in your rich text content, you can embed your model with your code and language:

CleanShot 2022-02-08 at 10 01 23

This approach has some downsides, though:

  1. You'll see an embed block instead of the actual code. I've discussed this with the GraphCMS team on their Slack channel, but I understand it's a hard one to fix. I created a naΓ―ve proposal, which would allow the user to expand the embeds inline, but I assume implementing something like this is not an easy task. Here's the idea I played around with:

image

  1. You can't create code blocks on the fly. When you want to add a new code block, you first need to go to the code block model and create content there, and only then you can embed the code block. The embed modal doesn't offer an action for creating new content:

CleanShot 2022-02-08 at 10 06 52

from rich-text.

jussivirtanen avatar jussivirtanen commented on August 25, 2024 2

Check out the custom embeds section on the documentation for an example of how to render embeds on the front end.

from rich-text.

OperationalFallacy avatar OperationalFallacy commented on August 25, 2024 1

That is exactly how I implemented it! I'm planning at some point to switch model to use version control system for what it does the best - edit and store code.

Should be trivial to parse the content even with the direct urls in the text to GitHub gists. Lots of options as soon as you start looking beyond the basic editor.

from rich-text.

jussivirtanen avatar jussivirtanen commented on August 25, 2024

I'm glad to hear you got it working. Feel free to close this issue.

from rich-text.

AzadKshitij avatar AzadKshitij commented on August 25, 2024

What to use to do sysntax highlighting ?
Im using next.js with @graphcms/rich-text-react-renderer to render the content.

from rich-text.

jussivirtanen avatar jussivirtanen commented on August 25, 2024

Hi @AzadKshitij, you can try Prism for syntax highlighting. There's an example on the React rich text renderer page.

from rich-text.

AzadKshitij avatar AzadKshitij commented on August 25, 2024

Hi, @jussivirtanen Thank you very much for your help.

from rich-text.

AzadKshitij avatar AzadKshitij commented on August 25, 2024

Hi @AzadKshitij, you can try Prism for syntax highlighting. There's an example on the React rich text renderer page.

How should i render this corectly. can you please provide some code for #57 (comment) method!

from rich-text.

robots4life avatar robots4life commented on August 25, 2024

What to use to do sysntax highlighting ?

https://highlightjs.org/

from rich-text.

OperationalFallacy avatar OperationalFallacy commented on August 25, 2024

https://github.com/react-syntax-highlighter/react-syntax-highlighter works very nicely with React-based frameworks

from rich-text.

thiagovilla avatar thiagovilla commented on August 25, 2024

Late to the party, but this is what I do:
image
image
I render it with gatsby-plugin-mdx. It works... πŸ€·πŸ»β€β™‚οΈ

from rich-text.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.