Help to deal with inline code vs fenced code without language hint after 9.0.0 #1346
-
Hello all, I am asking for help in figuring out how to reliably distinguish between an inline and a fenced code block that has no language hint since v9.0.0, when the When a fenced block has no language hint (which is allowed by the spec), So the "dance around fire" that I'm doing here is not 100% reliable. It misses a few situations:
components={{
// code: Used for inline code.
// pre > code: Code blocks are a code element with a pre as its direct ancestor.
code({ node, className, ...props }) {
const hasLang = /language-(\w+)/.exec(className || "");
let lang = hasLang ? hasLang[1] : null;
if (!lang
&& node
&& node.position
&& (node.position.start.column === 1)
&& (node.position.start.line !== node.position.end.line)) {
// What used to be hinted as 'inline' is not anymore, invalidating numerous old examples.
//
// The 8.0.7...9.0.0 diff says this (https://github.com/remarkjs/react-markdown/commit/434627686e21d4bcfb4301417e0da2bb851d4391):
//
// Previously, this project automatically passed different extra props to
// particular components.
// Those props are sometimes useful to some people, but not always useful
// to everyone.
//
// ### Remove extra props passed to certain components
//
// When overwriting components, these props are no longer passed:
//
// * `inline` on `code`:
// — create a plugin or use `pre` for the block
// We'll patch what seems to be 'inline' but is not, being a fenced block,
// with a default rendering language for highlighter.
lang = "txt";
} Please help to reliably distinguish so that I can pick how to render a real inline code vs a fenced code block that has no language hint. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 13 replies
-
This is not correct. This is an XY question. See for example https://xyproblem.info. You ask about something that does not exist. Please take a step back and ask your root question. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Yes, I am passing a code component. |
Beta Was this translation helpful? Give feedback.
-
There might be a need to provide a default language for all unspecified fenced code blocks. How would you accomplish that? |
Beta Was this translation helpful? Give feedback.
-
Thank you for the follow up! A consumer might have their reasons why they would require a block with unspecified language to be rendered with the Syntax highlighting. The one I mentioned; there might be others, like patching the language, or showing line numbers for even a simple text block. They might not want to maintain CSS defined once for Syntax highlighter and attempt to mimic it. The question boils down to how a consumer of the library is able to differentiate an inline code from a code block that has no class with the language hint in it? Is there a way without any other external plugins? |
Beta Was this translation helpful? Give feedback.
-
I am stuck and would greatly appreciate any kind assistance in figuring this out. https://stackblitz.com/edit/github-6nizfy-d935q8?file=src%2Fapp.tsx |
Beta Was this translation helpful? Give feedback.
I'd highly recommend reading https://unifiedjs.com/learn/
To understand what remark plugins are and how to make/maintain them.
Here is a one-liner example
https://stackblitz.com/edit/github-6nizfy-pv8tlr