反演原创标记下显示

这是叉子 jonasmerlin/ react-native-markdown-display (原始内容存档于2018-09-31) (英语) 它本身就是一个叉子 错失/反应-内在- 标记- 显示。这个用于我们自己的React Industrial应用程序,用于渲染丰富的文本。 对你也有用!
它是一个100%相容的CommonMark渲染器,一个反应-内在的标记下渲染器完成正确. 这是 没有 网络视图标记下渲染器,但渲染器使用其所有元素的本地组件。 这些组件可以根据需要被覆盖并样式.
与反应- 内置- 标记- 递归器相兼容
这是用来取代反应-内存-标记-降级-降级器,并有各种bug修正和增强- 由于新风格的规则如何运作, 可能需要一些调整, (中文(简体) ), 查看下面的样式
安装
雅伦
yarn add @cosmicmedia/react-native-markdown-display
国家预防机制
npm install -S @cosmicmedia/react-native-markdown-display
开始
import React from 'react';
import {SafeAreaView, ScrollView, StatusBar} from 'react-native';
import Markdown from 'react-native-markdown-display';
const copy = `# h1 Heading 8-)
**This is some bold text!**
This is normal text
`;
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={{height: '100%'}}>
<Markdown>{copy}</Markdown>
</ScrollView>
</SafeAreaView>
</>
);
};
export default App;
道具和函数
这个 <Markdown> 对象取下列常用道具:
| 财产 | 默认 | 需求 | 说明 |
|---|---|---|---|
children | 不详 | true | 缩放字符串来渲染,或者 预处理的树 |
style | 来源 | false | 一个反对推翻各种规则的样式, 参见下面样式一节 需要更多信息 |
mergeStyle | true | false | 如果真实, 当提供样式时, 单个项目与默认样式合并, 而不是覆盖 |
rules | 来源 | false | 规定如何制作每个减记项的规则对象, 见下文规则一节 需要更多信息 |
onLinkPress | import { Linking } from 'react-native'; 财务报告和审定财务报表 Linking.openURL(url); | false | 用于改变点击行为的处理器功能, 参见下面的处理链接部分 需要更多信息 |
debugPrintTree | false | false | 将打印出 AST 树到控制台, 帮助您看下调被翻译到什么 |
和一些额外的,较少使用的选项:
| 财产 | 默认 | 需求 | 说明 |
|---|---|---|---|
renderer | instanceOf(AstRenderer) | false | 用于指定自定义渲染器,不能使用自定义渲染器的规则或样式道具. |
markdownit | instanceOf(MarkdownIt) | false | 带有您的配置的自定义缩写实例, 默认为 MarkdownIt({typographer: true}) |
maxTopLevelChildren | null | false | 如果定义为数字,则仅先显示 n 许多顶级的孩子,然后会尝试出 topLevelMaxExceededItem |
topLevelMaxExceededItem | <Text key="dotdotdot">...</Text> | false | 何时 maxTopLevelChildren 被击中。 一定要给它一把钥匙! |
allowedImageHandlers | ['data:image/png;base64', 'data:image/gif;base64', 'data:image/jpeg;base64', 'https://', 'http://'] | false | 任何不从其中之一开始的图像都会有 defaultImageHandler 为它准备的值(除非 defaultImageHandler 在这样的情况下,它不会试图做任何事情) |
defaultImageHandler | http:// | false | 将准备到图像 URL,如果它不开始于 allowedImageHandlers 数组,如果设定为无效,它不会试图恢复,而只是不会产生任何东西. |
语法支持
标题
# h1 Heading 8-)
## h2 Heading
### h3 Heading
#### h4 Heading
##### h5 Heading
###### h6 Heading
| iOS系统 | 机器人 |
|---|---|
![]() | ![]() |
水平规则
Some text above
___
Some text in the middle
---
Some text below
| iOS系统 | 机器人 |
|---|---|
![]() | ![]() |
强调
**This is bold text**
__This is bold text__
*This is italic text*
_This is italic text_
~~Strikethrough~~
| iOS系统 | 机器人 |
|---|---|
![]() | ![]() |
块名
> Blockquotes can also be nested...
>> ...by using additional greater-than signs right next to each other...
> > > ...or with spaces between arrows.
| iOS系统 | 机器人 |
|---|---|
![]() | ![]() |
名单
Unordered
+ Create a list by starting a line with `+`, `-`, or `*`
+ Sub-lists are made by indenting 2 spaces:
- Marker character change forces new list start:
* Ac tristique libero volutpat at
+ Facilisis in pretium nisl aliquet. This is a very long list item that will surely wrap onto the next line.
- Nulla volutpat aliquam velit
+ Very easy!
Ordered
1. Lorem ipsum dolor sit amet
2. Consectetur adipiscing elit. This is a very long list item that will surely wrap onto the next line.
3. Integer molestie lorem at massa
Start numbering with offset:
57. foo
58. bar
| iOS系统 | 机器人 |
|---|---|
![]() | ![]() |
代码
Inline \`code\`
Indented code
// Some comments
line 1 of code
line 2 of code
line 3 of code
Block code "fences"
\`\`\`
Sample text here...
\`\`\`
Syntax highlighting
\`\`\` js
var foo = function (bar) {
return bar++;
};
console.log(foo(5));
\`\`\`
| iOS系统 | 机器人 |
|---|---|
![]() | ![]() |
表格
| Option | Description |
| ------ | ------------------------------------------------------------------------- |
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
Right aligned columns
| Option | Description |
| -----: | ------------------------------------------------------------------------: |
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
| iOS系统 | 机器人 |
|---|---|
![]() | ![]() |
链接
[link text](https://www.google.com)
[link with title](https://www.google.com 'title text!')
Autoconverted link https://www.google.com (enable linkify to see)
| iOS系统 | 机器人 |
|---|---|
![]() | ![]() |
图像


Like links, Images also have a footnote style syntax
![Alt text][id]
With a reference later in the document defining the URL location:
[id]: https://octodex.github.com/images/dojocat.jpg 'The Dojocat'
| iOS系统 | 机器人 |
|---|---|
![]() | ![]() |
类型替换
Enable typographer option to see result.
(c) (C) (r) (R) (tm) (TM) (p) (P) +-
test.. test... test..... test?..... test!....
!!!!!! ???? ,, -- ---
"Smartypants, double quotes" and 'single quotes'
| iOS系统 | 机器人 |
|---|---|
![]() | ![]() |
插件和扩展
用于 额外 语法支持可以使用任何可兼容的标记下- 它的插件添加 - 参见插件 从马克下调的文件。 一体化的例子如下:
步骤1
确定新组件,并将插件与已提供组件相融合。 我们可以使用 debugPrintTree 以查看我们执行的规则:
import React from 'react';
import { SafeAreaView, ScrollView, StatusBar } from 'react-native';
import Markdown, { MarkdownIt } from 'react-native-markdown-display';
import blockEmbedPlugin from 'markdown-it-block-embed';
const markdownItInstance =
MarkdownIt({typographer: true})
.use(blockEmbedPlugin, {
containerClassName: "video-embed"
});
const copy = `
# Some header
@[youtube](lJIrF4YjHfQ)
`;
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={{height: '100%'}}
>
<Markdown
debugPrintTree
markdownit={markdownItInstance}
>
{copy}
</Markdown>
</ScrollView>
</SafeAreaView>
</>
);
};
export default App;
在控制台中,我们将看到以下的成树:
body
-heading1
--textgroup
---text
-video
带有以下错误消息 :
Warning, unknown render rule encountered: video. 'unknown' render rule used (by default, returns null - nothing rendered)
步骤2
我们需要创建 发布规则 财务报告和审定财务报表 样式 处理此新 "视频" 组件
import React from 'react';
import {SafeAreaView, ScrollView, StatusBar} from 'react-native';
import Markdown, {MarkdownIt} from 'react-native-markdown-display';
import blockEmbedPlugin from 'markdown-it-block-embed';
const markdownItInstance = MarkdownIt({typographer: true}).use(
blockEmbedPlugin,
{
containerClassName: 'video-embed',
},
);
const copy = `
# Some header
@[youtube](lJIrF4YjHfQ)
`;
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={{height: '100%'}}>
<Markdown
debugPrintTree
markdownit={markdownItInstance}
style={{
video: {
color: 'red',
},
}}
rules={{
video: (node, children, parent, styles) => {
// examine the node properties to see what video we need to render
console.log(node); // expected output of this is in readme.md below this code snip
return (
<Text key={node.key} style={styles.video}>
Return a video component instead of this text component!
</Text>
);
},
}}>
{copy}
</Markdown>
</ScrollView>
</SafeAreaView>
</>
);
};
export default App;
制作有意义的东西所需的所有视频属性 都在节点上,像这样:
{type: "video", sourceType: "video", sourceInfo: {…}, sourceMeta: null, block: true, …}
attributes: {}
block: true
children: []
content: ""
index: 1
key: "rnmr_1720a98f540_video"
markup: "@[youtube](lJIrF4YjHfQ)"
sourceInfo:
service: YouTubeService
env: PluginEnvironment {md: MarkdownIt, options: {…}, services: {…}}
name: "youtube"
options:
height: 390
width: 640
serviceName: "youtube"
videoID: "lJIrF4YjHfQ"
videoReference: "lJIrF4YjHfQ"
sourceMeta: null
sourceType: "video"
tokenIndex: 5
type: "video"
其他调试
你可以做一些额外的调试 标记下的例子 吐出这样的:
import Markdown, {MarkdownIt} from 'react-native-markdown-display';
import blockEmbedPlugin from 'markdown-it-block-embed';
const markdownItInstance = MarkdownIt({typographer: true}).use(
blockEmbedPlugin,
{
containerClassName: 'video-embed',
},
);
const copy = `
# Some header
@[youtube](lJIrF4YjHfQ)
`;
// this shows you the tree that is used by the react-native-markdown-display
const astTree = markdownItInstance.parse(copy, {});
console.log(astTree);
//this contains the html that would be generated - not used by react-native-markdown-display but useful for reference
const html = markdownItInstance.render(copy);
console.log(html);
上面的代码会输出类似的东西:
astTree:
(4) [Token, Token, Token, Token]
0: Token {type: "heading_open", tag: "h1", attrs: null, map: Array(2), nesting: 1, …}
1: Token {type: "inline", tag: "", attrs: null, map: Array(2), nesting: 0, …}
2: Token {type: "heading_close", tag: "h1", attrs: null, map: null, nesting: -1, …}
3: Token {type: "video", tag: "div", attrs: null, map: Array(2), nesting: 0, …}
length: 4
html:
<h1>Some header</h1>
<div class="video-embed block-embed-service-youtube"><iframe type="text/html" src="//www.youtube.com/embed/lJIrF4YjHfQ" frameborder="0" width="640" height="390" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>
测试时全部标记下
测试所有情况下应用的样式都有效
Headings
# h1 Heading 8-)
## h2 Heading
### h3 Heading
#### h4 Heading
##### h5 Heading
###### h6 Heading
Horizontal Rules
Some text above
___
Some text in the middle
---
Some text below
Emphasis
**This is bold text**
__This is bold text__
*This is italic text*
_This is italic text_
~~Strikethrough~~
Blockquotes
> Blockquotes can also be nested...
>> ...by using additional greater-than signs right next to each other...
> > > ...or with spaces between arrows.
Lists
Unordered
+ Create a list by starting a line with `+`, `-`, or `*`
+ Sub-lists are made by indenting 2 spaces:
- Marker character change forces new list start:
* Ac tristique libero volutpat at
+ Facilisis in pretium nisl aliquet. This is a very long list item that will surely wrap onto the next line.
- Nulla volutpat aliquam velit
+ Very easy!
Ordered
1. Lorem ipsum dolor sit amet
2. Consectetur adipiscing elit. This is a very long list item that will surely wrap onto the next line.
3. Integer molestie lorem at massa
Start numbering with offset:
57. foo
58. bar
Code
Inline \`code\`
Indented code
// Some comments
line 1 of code
line 2 of code
line 3 of code
Block code "fences"
\`\`\`
Sample text here...
\`\`\`
Syntax highlighting
\`\`\` js
var foo = function (bar) {
return bar++;
};
console.log(foo(5));
\`\`\`
Tables
| Option | Description |
| ------ | ----------- |
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
Right aligned columns
| Option | Description |
| ------:| -----------:|
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
Links
[link text](https://www.google.com)
[link with title](https://www.google.com "title text!")
Autoconverted link https://www.google.com (enable linkify to see)
Images


Like links, Images also have a footnote style syntax
![Alt text][id]
With a reference later in the document defining the URL location:
[id]: https://octodex.github.com/images/dojocat.jpg "The Dojocat"
Typographic Replacements
Enable typographer option to see result.
(c) (C) (r) (R) (tm) (TM) (p) (P) +-
test.. test... test..... test?..... test!....
!!!!!! ???? ,, -- ---
"Smartypants, double quotes" and 'single quotes'
规则和样式
怎么样的风格的东西
文本样式的应用方式使得管理全局样式的改变更加方便,同时也允许对单个元素进行细微的调和.
想想CSS中的应用风格等执行. 更改 body 效果一切, 但可以在样式 / 组件树下进一步覆盖 .
格式化“ text” 时要小心 : 文本规则并不适用于所有文本,最显著的是列出点。 如果你想,例如,所有文本的颜色,请修改 body 样式.
示例
import React from 'react';
import {SafeAreaView, ScrollView, StatusBar} from 'react-native';
import Markdown from 'react-native-markdown-display';
const copy = `
This is some text which is red because of the body style, which is also really small!
\`\`\`
//This is a code block woooo
const cool = () => {
console.log('????');
};
\`\`\`
and some more small text
# This is a h1
## this is a h2
### this is a h3
`;
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={{height: '100%'}}>
<Markdown
style={{
body: {color: 'red', fontSize: 10},
heading1: {color: 'purple'},
code_block: {color: 'black', fontSize: 14},
}}>
{copy}
</Markdown>
</ScrollView>
</SafeAreaView>
</>
);
};
export default App;
样式
样式用来推翻某些规则的样式。 现有执行工作是: 这里
注: 默认样式与现有执行合并,以更改此样式,参见 mergeStyle 道具
实例
import React from 'react';
import {SafeAreaView, ScrollView, StatusBar, StyleSheet} from 'react-native';
import Markdown from 'react-native-markdown-display';
const styles = StyleSheet.create({
heading1: {
fontSize: 32,
backgroundColor: '#000000',
color: '#FFFFFF',
},
heading2: {
fontSize: 24,
},
heading3: {
fontSize: 18,
},
heading4: {
fontSize: 16,
},
heading5: {
fontSize: 13,
},
heading6: {
fontSize: 11,
},
});
const copy = `
# h1 Heading 8-)
## h2 Heading 8-)
### h3 Heading 8-)
| Option | Description |
| ------ | ----------- |
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
`;
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={{height: '100%'}}>
<Markdown style={styles}>{copy}</Markdown>
</ScrollView>
</SafeAreaView>
</>
);
};
export default App;
规则
规则用于指定要如何显示某些元素。 现有执行工作是: 这里
实例
import React from 'react';
import {SafeAreaView, ScrollView, StatusBar, Text} from 'react-native';
import Markdown from 'react-native-markdown-display';
const rules = {
heading1: (node, children, parent, styles) => (
<Text key={node.key} style={[styles.heading, styles.heading1]}>
>> H1 TEXT HERE >> "{children}"
</Text>
),
heading2: (node, children, parent, styles) => (
<Text key={node.key} style={[styles.heading, styles.heading2]}>
>> H2 TEXT HERE >> "{children}"
</Text>
),
heading3: (node, children, parent, styles) => (
<Text key={node.key} style={[styles.heading, styles.heading3]}>
>> H3 TEXT HERE >> "{children}"
</Text>
),
};
const copy = `
# h1 Heading 8-)
## h2 Heading 8-)
### h3 Heading 8-)
| Option | Description |
| ------ | ----------- |
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
`;
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={{height: '100%'}}>
<Markdown rules={rules}>{copy}</Markdown>
</ScrollView>
</SafeAreaView>
</>
);
};
export default App;
所有规则及其相关风格:
| 规则 | 样式 |
|---|---|
body | body |
heading1 | heading1 |
heading2 | heading2 |
heading3 | heading3 |
heading4 | heading4 |
heading5 | heading5 |
heading6 | heading6 |
hr | hr |
strong | strong |
em | em |
s | s |
blockquote | blockquote |
bullet_list | bullet_list |
ordered_list | ordered_list |
list_item | list_item - 这是一个特殊的案例,它包含一组伪类 不符合渲染规则: ordered_list_icon, (中文(简体) ), ordered_list_content, (中文(简体) ), bullet_list_icon, (中文(简体) ), bullet_list_content |
code_inline | code_inline |
code_block | code_block |
fence | fence |
table | table |
thead | thead |
tbody | tbody |
th | th |
tr | tr |
td | td |
link | link |
blocklink | blocklink |
image | image |
text | text |
textgroup | textgroup |
paragraph | paragraph |
hardbreak | hardbreak |
softbreak | softbreak |
pre | pre |
inline | inline |
span | span |
处理链接
链接,默认情况下,将与 import { Linking } from 'react-native'; 导入和 Linking.openURL(url); 电话.
可以通过两种方式之一推翻这种行为:
在 LinkPress 回电
import React from 'react';
import {SafeAreaView, ScrollView, StatusBar} from 'react-native';
import Markdown from 'react-native-markdown-display';
const copy = `[This is a link!](https://github.com/iamacup/react-native-markdown-display/)`;
const onLinkPress = (url) => {
if (url) {
// some custom logic
return false;
}
// return true to open with `Linking.openURL
// return false to handle it yourself
return true;
};
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={{height: '100%'}}>
<Markdown onLinkPress={onLinkPress}>{copy}</Markdown>
</ScrollView>
</SafeAreaView>
</>
);
};
export default App;
使用自定义规则
您需要覆盖一个或两个 link 财务报告和审定财务报表 blocklink,原始的降级可以是 在这里找到的
像这样 yourCustomHandlerFunctionOrLogicHere数字 :
import React from 'react';
import {SafeAreaView, ScrollView, StatusBar, Text} from 'react-native';
import Markdown from 'react-native-markdown-display';
const copy = `[This is a link!](https://github.com/iamacup/react-native-markdown-display/)`;
const rules = {
link: (node, children, parent, styles) => {
return (
<Text
key={node.key}
style={styles.link}
onPress={() =>
yourCustomHandlerFunctionOrLogicHere(node.attributes.href)
}>
{children}
</Text>
);
},
};
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={{height: '100%'}}>
<Markdown rules={rules}>{copy}</Markdown>
</ScrollView>
</SafeAreaView>
</>
);
};
export default App;
禁用标记下的特定类型
您可以通过下面的减记属性来解析任何类型的减记,这在移动环境中非常有用. 请注意,为了方便起见,我们还出口 MarkdownIt 实例,所以您不必直接将它作为项目依赖性来删除某些类型的平分.
此示例将停止图像和链接 .
import React from 'react';
import {SafeAreaView, ScrollView, StatusBar, Text} from 'react-native';
import Markdown, {MarkdownIt} from 'react-native-markdown-display';
const copy = `
# This heading will show with formatting
[but this link will just](be displayed as this text)
`;
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={{height: '100%'}}>
<Markdown
markdownit={MarkdownIt({typographer: true}).disable([
'link',
'image',
])}>
{copy}
</Markdown>
</ScrollView>
</SafeAreaView>
</>
);
};
export default App;
一份完整的可以关闭的东西列表 这里
预处理
可能需要在这个库外预先处理数据(相关讨论) (中文(简体) ). 因此,可以直接通过一棵AST树,像这样的孩子:
import React from 'react';
import {SafeAreaView, ScrollView, StatusBar, Text} from 'react-native';
import Markdown, {
MarkdownIt,
tokensToAST,
stringToTokens,
} from 'react-native-markdown-display';
const markdownItInstance = MarkdownIt({typographer: true});
const copy = `
# Hello this is a title
This is some text with **BOLD!**
`;
const ast = tokensToAST(stringToTokens(copy, markdownItInstance));
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={{height: '100%'}}>
<Markdown>{ast}</Markdown>
</ScrollView>
</SafeAreaView>
</>
);
};
export default App;
其他说明
这是叉子 反应- 内向- 标记- 投降器图书馆已经很久没有更新了 所以我从图书馆拿走了所有未解决的拉动请求 并进行了测试.



















