sibiraj-s / marked-react Goto Github PK
View Code? Open in Web Editor NEW⚛️ Render Markdown as React components
Home Page: https://sibiraj-s.github.io/marked-react/
License: MIT License
⚛️ Render Markdown as React components
Home Page: https://sibiraj-s.github.io/marked-react/
License: MIT License
The tags are being parsed as text instead of html tags to highlight the text.
When I create a custom renderer
const renderer = {
paragraph(text: string) {
return (
<p key={this.elementId} className={s.paragraph}>
{text}
</p>
)
},
}
I've got an error Property 'elementId' does not exist on type '{ paragraph(text: string): Element; }'.ts(2339)
. What I missed to use renderer with TS?
Writing DOM.purify inside renderer, right before returning the parsed markup leads to problems like showing [object Object] for things like bold and italics.
const renderer = {
paragraph(text) {
console.log("text =", text)
// let cleanHTML = DOMPurify.sanitize(text, { USE_PROFILES: { html: true } });
return <MathJax key={uniqueId()}>{text}</MathJax>
// return <span>hahah lol</span>
},
html(text) {
let cleanHTML = DOMPurify.sanitize(text, { USE_PROFILES: { html: true } });
return <span key={"title-span"} dangerouslySetInnerHTML={{ __html: cleanHTML }}></span>;
}
}
Where do you suggest we purify the input?
Thx for lib.
How can i add a custom extension?
In "marked" lib i can use marked.use().
The component works without them, so it seems they should be marked as optional.
Neat lib, thanks 👍
What's a good way to parse inline so that parent paragraph does not appear? (eg: <p>stuff</p>
→ stuff
)
I was implementing markdown parser in my project and ran into a problem where links with relative paths were redirecting to a page and clearing state (acting as standard anchors) due to page reload. React Router Link component or useNavigate() secures the page reload and keeps the state. I was suggested to use marked-react for this but it didn't solve my problem.
Could you please provide solution for replacing anchors with Link component?
Description
After installing the latest version of marked-react
, I am seeing a runtime error (see "Additional Context" below). The source-map-loader
package is looking for files in C:\myproject\node_modules\marked-react\src\
, but the src
directory is missing.
I have tried uninstalling and reinstalling the package, but the problem persists.
Steps to reproduce
marked-react
package in a projectwebpack-dev-server --mode development
; see error (below)src
is not in node_modules\marked-react
directoryEnvironment
Operating system: Windows
Package manager: npm
marked-react package version: 1.3.1
source-map-loader: 3.0.2 || 4.0.1
Additional context
Here is the full runtime error:
Compiled with problems:
WARNING in ./node_modules/marked-react/dist/marked-react.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from 'C:\myproject\node_modules\marked-react\src\Markdown.ts' file: Error: ENOENT: no such file or directory, open 'C:\myproject\node_modules\marked-react\src\Markdown.ts'
WARNING in ./node_modules/marked-react/dist/marked-react.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from 'C:\myproject\node_modules\marked-react\src\ReactParser.ts' file: Error: ENOENT: no such file or directory, open 'C:\myproject\node_modules\marked-react\src\ReactParser.ts'
WARNING in ./node_modules/marked-react/dist/marked-react.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from 'C:\myproject\node_modules\marked-react\src\ReactRenderer.ts' file: Error: ENOENT: no such file or directory, open 'C:\myproject\node_modules\marked-react\src\ReactRenderer.ts'
WARNING in ./node_modules/marked-react/dist/marked-react.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from 'C:\myproject\node_modules\marked-react\src\helpers.ts' file: Error: ENOENT: no such file or directory, open 'C:\myproject\node_modules\marked-react\src\helpers.ts'
The library doesn't Support Nested list as All List gets rendered as single li even if they are alledged as
- test1
- test2
It would be nice if there was a way to pass data to the renderer via the renderer
prop.
Perhaps, instead of passing an object as a renderer, one could have the additional option of passing a function that returns the renderer object. This way I can pass in additional data from the component where I use <Markdown>
component in, with maybe a data
prop. This is just a suggestion of how it could work.
I find this useful if I want to style the output of a renderer token based on some data inside my component.
E.g.
If I had a <Paragraph>
component that renders markdown text as a paragraph and I want to be able style that render output based on if the paragraph is supposed to be grayed out(this is passed as a prop to my component).
const Paragraph = (props) => {
// suggested/proposed renderer function that returns renderer object
let rendererFn = (data) => {
let isGreyedOut = data.grey;
return {
paragraph(text) {
React.createElement('p', {className: `${isGreyedOut && 'text-grey'}`}, text)
}
}
}
return (
{/* data from my component is passed to the <Marked> 'renderer' prop(which now receives a function) via the proposed 'data' prop */}
<Marked renderer={rendererFn} data={{isGreyedOut: props.isGreyedOut}} value={props.text}/>
)
}
Don't know if this makes any sense...
Thank you for the great extension! It really helps!
So, I need to parse some custom markdown tokens from own tokenizer extension. I can add functions to custom renderer but is there an option to make parser recognize new token types?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.