在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):probablyup/markdown-to-jsx开源软件地址(OpenSource Url):https://github.com/probablyup/markdown-to-jsx开源编程语言(OpenSource Language):TypeScript 99.1%开源软件介绍(OpenSource Introduction):markdown-to-jsx The most lightweight, customizable React markdown component.
All this clocks in at around 5 kB gzipped, which is a fraction of the size of most other React markdown components. Requires React >= 0.14. InstallationInstall npm i markdown-to-jsx Usage
ES6-style usage*: import Markdown from 'markdown-to-jsx';
import React from 'react';
import { render } from 'react-dom';
render(<Markdown># Hello world!</Markdown>, document.body);
/*
renders:
<h1>Hello world!</h1>
*/ * NOTE: JSX does not natively preserve newlines in multiline text. In general, writing markdown directly in JSX is discouraged and it's a better idea to keep your content in separate .md files and require them, perhaps using webpack's raw-loader. Parsing Optionsoptions.forceBlockBy default, the compiler will try to make an intelligent guess about the content passed and wrap it in a Hello. _Beautiful_ day isn't it? But this string would be considered "block" due to the existence of a header tag, which is a block-level HTML element: # Whaddup? However, if you really want all input strings to be treated as "block" layout, simply pass <Markdown options={{ forceBlock: true }}>Hello there old chap!</Markdown>;
// or
compiler('Hello there old chap!', { forceBlock: true });
// renders
<p>Hello there old chap!</p>; options.forceInlineThe inverse is also available by passing <Markdown options={{ forceInline: true }}># You got it babe!</Markdown>;
// or
compiler('# You got it babe!', { forceInline: true });
// renders
<span># You got it babe!</span>; options.wrapperWhen there are multiple children to be rendered, the compiler will wrap the output in a const str = '# Heck Yes\n\nThis is great!'
<Markdown options={{ wrapper: 'article' }}>
{str}
</Markdown>;
// or
compiler(str, { wrapper: 'article' });
// renders
<article>
<h1>Heck Yes</h1>
<p>This is great!</p>
</article> Other useful recipesTo get an array of children back without a wrapper, set compiler('One\n\nTwo\n\nThree', { wrapper: null });
// returns
[
(<p>One</p>),
(<p>Two</p>),
(<p>Three</p>)
] To render children at the same DOM level as options.forceWrapperBy default, the compiler does not wrap the rendered contents if there is only a single child. You can change this by setting // Using `forceWrapper` with a single, inline child…
<Markdown options={{ wrapper: 'aside', forceWrapper: true }}>
Mumble, mumble…
</Markdown>
// renders
<aside>Mumble, mumble…</aside> options.overrides - Override Any HTML Tag's RepresentationPass the import Markdown from 'markdown-to-jsx';
import React from 'react';
import { render } from 'react-dom';
// surprise, it's a div instead!
const MyParagraph = ({ children, ...props }) => (
<div {...props}>{children}</div>
);
render(
<Markdown
options={{
overrides: {
h1: {
component: MyParagraph,
props: {
className: 'foo',
},
},
},
}}
>
# Hello world!
</Markdown>,
document.body
);
/*
renders:
<div class="foo">
Hello World
</div>
*/ If you only wish to provide a component override, a simplified syntax is available: {
overrides: {
h1: MyParagraph,
},
} Depending on the type of element, there are some props that must be preserved to ensure the markdown is converted as intended. They are:
Any conflicts between passed Some element mappings are a bit different from other libraries, in particular:
options.overrides - Rendering Arbitrary React ComponentsOne of the most interesting use cases enabled by the HTML syntax processing in By adding an override for the components you plan to use in markdown documents, it's possible to dynamically render almost anything. One possible scenario could be writing documentation: import Markdown from 'markdown-to-jsx';
import React from 'react';
import { render } from 'react-dom';
import DatePicker from './date-picker';
const md = `
# DatePicker
The DatePicker works by supplying a date to bias towards,
as well as a default timezone.
<DatePicker biasTowardDateTime="2017-12-05T07:39:36.091Z" timezone="UTC+5" />
`;
render(
<Markdown
children={md}
options={{
overrides: {
DatePicker: {
component: DatePicker,
},
},
}}
/>,
document.body
);
In the following case, import Markdown from 'markdown-to-jsx';
import React from 'react';
import { render } from 'react-dom';
import DatePicker from './date-picker';
const md = `
# DatePicker
The DatePicker works by supplying a date to bias towards,
as well as a default timezone.
<DatePicker
biasTowardDateTime="2017-12-05T07:39:36.091Z"
timezone="UTC+5"
startTime={1514579720511}
/>
`;
render(
<Markdown
children={md}
options={{
overrides: {
DatePicker: {
component: DatePicker,
},
},
}}
/>,
document.body
); Another possibility is to use something like recompose's import Markdown from 'markdown-to-jsx';
import React from 'react';
import { render } from 'react-dom';
import withProps from 'recompose/withProps';
import DatePicker from './date-picker';
const DecemberDatePicker = withProps({
range: {
start: new Date('2017-12-01'),
end: new Date('2017-12-31'),
},
timezone: 'UTC+5',
})(DatePicker);
const md = `
# DatePicker
The DatePicker works by supplying a date to bias towards,
as well as a default timezone.
<DatePicker
biasTowardDateTime="2017-12-05T07:39:36.091Z"
timezone="UTC+5"
startTime={1514579720511}
/>
Here's an example of a DatePicker pre-set to only the month of December:
<DecemberDatePicker />
`;
render(
<Markdown
children={md}
options={{
overrides: {
DatePicker,
DecemberDatePicker,
},
}}
/>,
document.body
); options.createElement - Custom React.createElement behaviorSometimes, you might want to override the import Markdown from 'markdown-to-jsx';
import React from 'react';
import { render } from 'react-dom';
const md = `
# Hello world
`;
render(
<Markdown
children={md}
options={{
createElement(type, props, children) {
return (
<div className="parent">
{React.createElement(type, props, children)}
</div>
);
},
}}
/>,
document.body
); options.slugifyBy default, a lightweight deburring function is used to generate an HTML id from headings. You can override this by passing a function to <Markdown options={{ slugify: str => str }}># 中文</Markdown>;
// or
compiler('# 中文', { slugify: str => str });
// renders:
<h1 id="中文">中文</h1> options.namedCodesToUnicodeBy default only a couple of named html codes are converted to unicode characters:
Some projects require to extend this map of named codes and unicode characters. To customize this list with additional html codes pass the option namedCodesToUnicode as object with the code names needed as in the example below: <Markdown options={{ namedCodesToUnicode: {
le: '\u2264',
ge: '\u2265',
} }}>This text is ≤ than this text.</Markdown>;
// or
compiler('This text is ≤ than this text.', namedCodesToUnicode: {
le: '\u2264',
ge: '\u2265',
});
// renders:
<p>This text is ≤ than this text.</p> options.disableParsingRawHTMLBy default, raw HTML is parsed to JSX. This behavior can be disabled with this option. <Markdown options={{ disableParsingRawHTML: true }}>
This text has <span>html</span> in it but it won't be rendered
</Markdown>;
// or
compiler('This text has <span>html</span> in it but it won't be rendered', { disableParsingRawHTML: true });
// renders:
<span>This text has <span>html</span> in it but it won't be rendered</span> Syntax highlightingSome syntax highlighters require you to specify the language. The language of the code fence is
forwarded in the className prop of the element used for const Code = ({className, children}) => {
const language = className.replace("lang-", "");
return (
<SyntaxHighlighter language={language}>
<code>{children}</code>
</SyntaxHighlighter>
);
} Getting the smallest possible bundle sizeMany development conveniences are placed behind Here are instructions for some of the popular bundlers: Usage with PreactEverything will work just fine! Simply Alias GotchasSignificant indentation inside arbitrary HTMLPeople usually write HTML like this: <div>
Hey, how are you?
</div> Note the leading spaces before the inner content. This sort of thing unfortunately clashes with existing markdown syntaxes since 4 spaces === a code block and other similar collisions. To get around this, <div>
# Hello
How are you?
</div> The two leading spaces in front of "# Hello" would be left-trimmed from all lines inside the HTML block. In the event that there are varying amounts of indentation, only the amount of the first line is trimmed.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论