code-syntax-block icon indicating copy to clipboard operation
code-syntax-block copied to clipboard

Question: Is there a way of capturing the class of an existing code block

Open caraya opened this issue 7 years ago • 4 comments

Is there a way for this block to capture the class of an existing code block? Is this what #4 is designed to support?

If this solves the issue I can then close wordpress/gutenberg/issues/5392

BTW, thanks for tackling this one!

caraya avatar Apr 08 '18 20:04 caraya

I think it depends on what you mean by an existing code block and waht the class looks like.

My idea for Issue #4 in this repo was auto converting from markdown pasted in like: ```php and setting the class name to PHP.

I need to read up more on how transform works, and how it might work with extending a core block. It seems like it could be something to do from old posts markup depending on the structure of the code.

mkaz avatar Apr 09 '18 00:04 mkaz

Something similar... I've been evaluating using Gutenberg in a blog that has several codeblocks fenced with ``` and the language name, the same way you're thinking about doing it for pasted content. What I'm not sure is whether Gutenberg will work with the converted HTML or the raw Markdown.

caraya avatar Apr 09 '18 04:04 caraya

Doing a little more research, it looks like pasted in markdown is handled via the RichText component and specifically this function strips out the extra language info, since the default code block does not have a language attribute.

I'll need to think more about how this could work. I commented on a similar issue here, with regards to table losing class names. Maybe during conversion there could be some rules to set the block's additional class field, if that gets set it might be easier to hook into.

At least it is pretty straight-forward to go through and set the language on code blocks once they get imported., I've done it for old posts I'm converting on my mkaz.blog site. I convert the classic post to blocks and then click into each code block and select the language.

mkaz avatar Apr 10 '18 00:04 mkaz

I agree it's possible... it's just frustrating.

Thank you for taking the time to look into this.

caraya avatar Apr 10 '18 00:04 caraya