-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Blocks: Shortcode: Support multi-line shortcodes #4942
Conversation
The main hurdle here is that, during content serialization, line breaks inside the shortcode block are destroyed by the beautifier: gutenberg/blocks/api/serializer.js Line 199 in 62d746f
Now, there's ways to handle this. One is that there may be other block types out there where total control of a block's inner HTML is desired. For this, something like // in core-blocks/shortcode/index.js
supports: { …, rawContent: true }
// in `blocks/api/serializer.js`'s `getBlockContent`:
return (
getUnknownTypeHandlerName() === block.name ||
hasBlockSupport( blockType, 'rawContent' ) ||
! saveContent
) ? saveContent : getBeautifulContent( saveContent ); However, with @aduth and @iseulde we've wondered whether it still made sense, going forward, to systematically beautify blocks' inner HTML. If it doesn't, then we shouldn't rush to add a new +++ core-blocks/index.js
setDefaultBlockName( paragraph.name );
setUnknownTypeHandlerName( freeform.name );
+addRawContentBlockType( shortcode.name ); return (
getUnknownTypeHandlerName() === block.name ||
isRawContentBlockType( block.name ) ||
! saveContent
) ? saveContent : getBeautifulContent( saveContent ); ? |
bba5d6b
to
15a3736
Compare
The last two commits work and represent the two approaches I described above. |
In general I wish we'd just solve this from the serializer (no additional processing on Which is to say... can you tell me what beauty means to you? |
The beautifier is not really about beauty... more about readability, no? Without it the text editor is not that useful? Is there no option to preserve line breaks with it? |
There is actually a https://github.com/beautify-web/js-beautify#css--html To be added as an option to gutenberg/blocks/api/serializer.js Lines 168 to 174 in f037e90
Since the element serializer outputs no newlines of its own (except for those hard-coded), I expect this should work without conflict. |
@@ -45,7 +44,7 @@ export const settings = { | |||
text: { | |||
type: 'string', | |||
shortcode: ( attrs, { content } ) => { | |||
return content; | |||
return content.replace( /<br \/>/g, '' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do we know if these are added by the browser? Guess we would need to check if it's plain text, and only remove then?
Btw, the current editor also destroys the following.
[bwcsv]One,Two,Three
A,B,C
D,E,F
[/bwcsv]
becomes
<p>[bwcsv]One,Two,Three A,B,C D,E,F [/bwcsv]</p>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And if pasted in TinyMCE it becomes:
<p>[bw_csv]One,Two,Three<br />A,B,C<br />D,E,F<br />[/bw_csv]</p>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, the current editor also destroys the following.
Not if manually entered, right?
Regardless, a block whose purpose is to hold shortcodes should be capable of preserving arbitrary formatting, IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should preserve it, agreed. But this line here is about input. Now I just realised this is actually the Markdown converter inserting these line break elements? With the simpleLineBreaks
option. 😰
|
Note: Alternative solution at #6716. |
I'm happier with a broader fix like #6716, so I'll close this one. If we change our minds, we can open it back. |
Description
Partly fixes #4456
In progress: this doesn't yet preserve newlines within a Shortcode block if reloading the block (when switching between Visual and Code modes or by reloading Gutenberg).When converting content to blocks (either explicitly via the Convert to blocks action or by pasting content), content fragments that are recognized as shortcodes (but don't have a specific block to handle it) are then sent through the generic Shortcode block's
'shortcode'
transform. Since raw content is canonically treated as HTML, the Shortcode transform receives any newline as<br />
. This PR converts these into\n
inside the shortcode transform.This was an issue because the Shortcode block intentionally treats its contents literally. This is because it is meant to be a place where HTML entities are not decoded, so as to better support the extent of WordPress shortcode usage. See #3609 for history.
How Has This Been Tested?
and make sure the process creates a Shortcode block that respects the original line breaks.
Caveat: this is a remaining issue to be solved concerning pasting for shortcodes containing underscores. In other words, the issue author's
[bw_csv]
shortcode will not be correctly parsed upon pasting. It will, however, be handled correctly when converting from classic.<br />
will not be treated as a newline.Screenshots (jpeg or gifs if applicable):
Types of changes
Checklist: