[lexical-code-prism][lexical-code-shiki][lexical-playground] Feature: Allow null Tokenizer.defaultLanguage to preserve markdown ``` round-trip#8553
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
cebb630 to
f9e1815
Compare
f9e1815 to
7f63844
Compare
etrepum
left a comment
There was a problem hiding this comment.
I wonder if this could be done in a backwards compatible way, perhaps by allowing defaultLanguage to be explicitly null or undefined in the extension configurations?
We should probably go ahead and deprecate getDefaultCodeLanguage while we're in here since it's unused internally and doesn't really have any bearing on how things work. DEFAULT_CODE_LANGUAGE could probably also be deprecated although that is currently used internally.
7f63844 to
fde46d6
Compare
|
Switched to the
|
fde46d6 to
e5ab1e8
Compare
e5ab1e8 to
09afe60
Compare
09afe60 to
7929244
Compare
7929244 to
01d7421
Compare
01d7421 to
7b0dc75
Compare
etrepum
left a comment
There was a problem hiding this comment.
Do we still have e2e tests that explicitly set a language in the markdown blocks to make sure these highlighters are still working correctly with the playground config in those cases?
| @@ -56,6 +56,7 @@ export type SerializedCodeNode = Spread< | |||
| >; | |||
|
|
|||
| export const DEFAULT_CODE_LANGUAGE = 'javascript'; | |||
There was a problem hiding this comment.
I think this can be marked as @internal - it's not really an API and doesn't make sense when it's overridden in the configs
There was a problem hiding this comment.
Switched to @internal.
| >; | ||
|
|
||
| export const DEFAULT_CODE_LANGUAGE = 'javascript'; | ||
| /** @deprecated Read {@link DEFAULT_CODE_LANGUAGE} directly. */ |
There was a problem hiding this comment.
I don't think we need to point people anywhere else, maybe just say it's configurable in the extensions
There was a problem hiding this comment.
Trimmed the JSDoc — just says it's configurable through the extensions now.
| * compatible with the indent / shift-lines handlers that only accept | ||
| * CodeHighlightNode + TabNode + LineBreakNode inside a CodeNode. | ||
| */ | ||
| function $plainifyCodeContent(text: string): LexicalNode[] { |
There was a problem hiding this comment.
We should unify these into @lexical/code-core since we have the code repeated verbatim in each highlighter package
There was a problem hiding this comment.
Moved $plainifyCodeContent to @lexical/code-core and imported from both highlighter packages.
… Allow null Tokenizer.defaultLanguage to preserve markdown ``` round-trip
7b0dc75 to
13ebc2c
Compare
|
Yes — |
Description
Markdown
(no info string) imported into a wysiwyg editor with the Prism or Shiki highlighter active permanently rewrote the `CodeNode`'s language to the tokenizer's default ('javascript'), and the next markdown export emittedjavascript instead of preserving the round-trip.Per the maintainer suggestion on this PR, this widens
Tokenizer.defaultLanguagefromstringtostring | nullso consumers can opt out of the implicit fallback. When set tonull:__languageasundefined(nosetLanguagemutation)getIsSyntaxHighlightSupportedreportsfalse, socreateDOMemits nodata-language/data-highlight-languageattributes$tokenizereturns a plain split of the text (TextNode + LineBreakNode + TabNode) instead of highlight tokens, so the code block renders as plain monospace with real line breaks — matching what GitHub / Slack / VS Code do for ``` with no languagePrismTokenizer/ShikiTokenizerkeep their existingdefaultLanguage: DEFAULT_CODE_LANGUAGEso default consumers see no behavior change. Playground opts in by spreading{...PrismTokenizer, defaultLanguage: null}(and the Shiki equivalent) into theCodeHighlightExtensiondependency.Playground toolbar and CodeActionMenu gained UX support for the unset state — the language dropdown shows
(No language)as the label and as a selectable item that callssetLanguage(null)(which still zaps toundefinedper existing semantics, but lets users return a code block to the unset state without going through markdown). CodeActionMenu falls back to the syntax-highlight default forcanBePrettier/ the friendly name so Prettier still works on unset blocks.Cleanup
getDefaultCodeLanguagegained@deprecated— it is unused internally andDEFAULT_CODE_LANGUAGEcan be read directly.DEFAULT_CODE_LANGUAGEis still used internally so left alone.Backwards compatibility
Tokenizer.defaultLanguagewidens fromstringtostring | null. Existing consumers passing astringare unaffected; consumers readingtokenizer.defaultLanguageneed a null guard if they handle the opt-out case. The defaultPrismTokenizer/ShikiTokenizerinstances are unchanged, so any consumer that registersCodePrismExtension/CodeShikiExtensionwithout overriding the tokenizer keeps the old behavior.Closes #7235
Test plan
pnpm tsc --noEmit -p tsconfig.jsonclean.pnpm flowclean.pnpm vitest run --project unit— 2599 passed, 1 skipped.npx prettier --checkclean /pnpm eslintclean.CodePrismNullDefaultLanguage.test.ts,CodeShikiNullDefaultLanguage.test.ts) register the highlight withdefaultLanguage: nulland confirmgetLanguage()staysundefinedafter the transform runs.(no lang) → wysiwyg → plain monospace (no highlight tokens), toolbar shows `(No language)`; M-toggle back → markdown still.python → wysiwyg → tokens + toolbar `Python`; M-toggle back →python./codeslash menu → empty code block → toolbar(No language), no highlight (default opted out).(No language)item on a highlighted Python block → highlight clears, block becomes plain; markdown export ```.(No language)block → format applied,\nrendered as real line breaks (not literal characters).CodeBlock,CodeActionMenu,Indentation,Markdown,Tab,1384-insert-nodes) —data-language="javascript"/data-highlight-language="javascript"no longer present when the block was created via markdown ``` shortcut.