Markdown 基本用法说明

Markdown 基本用法说明

标题Header

两种表现形式:
1. #,一个#就是一级标题即h1,######就是h6[atx]
2. 在文本下加===,—,前为一级,后者为二级[Setext]

A First Level Header

A Second Level Header

Header 3

输出为HTML:

<h1>A First Level Header</h1>
<h2>A Second Level Header</h2>

<h3>Header 3</h3>

段落

一个段落是由一个以上的连接的行句组成,而一个以上的空行则会划分出不同的段落(空行的定义是显示上看起来像是空行,就被视为空行,例如有一行只有空白和 tab,那该行也会被视为空行),一般的段落不需要用空白或换行缩进。

Now is the time for all good men to come to
the aid of their country. This is just a

regular paragraph.
The quick brown fox jumped over the lazy
dog’s back.

<p>Now is the time for all good men to come to
the aid of their country. This is just a
regular paragraph.</p>
<p>The quick brown fox jumped over the lazy
dog's back.</p>

区块引用

使用’ > ’ 尖角号表示区块引用。

This is a blockquote.

This is the second paragraph in the blockquote.

This is an H2 in a blockquote

<blockquote>
<p>This is a blockquote.</p>
<p>This is the second paragraph in the blockquote.</p>
<h2>This is an H2 in a blockquote</h2>
</blockquote>

修辞和强调

  • 一个*或者_表示斜体
  • 两个**或者两个__表示粗体
  • 三个* 或者_表示粗斜体
  • ~~表示删除线

Some of these words are emphasized.
Some of these words are emphasized also.

Use two asterisks for strong emphasis.
Or, if you prefer, use two underscores instead.

And this is the strikethrough.

<p>Some of these words <em>are emphasized</em>.
Some of these words <em>are emphasized also</em>.</p>
<p>Use two asterisks for <strong>strong emphasis</strong>.
Or, if you prefer, <strong>use two underscores instead</strong>.</p>
<p>And this is the <s>strikethrough</s></p>

超链接

Markdown 支持两种形式的链接语法: 行内式和参考式,两种都是使用中括号来把文字转成链接,行内式一般使用较多。

行内式

行内形式是直接在后面用括号直接接上链接

This is an example link.

This is an example link.

输出为HTML

<p>This is an <a href="http://example.com/">example link</a>.</p>
<p>This is an <a href="http://example.com/" title="With a Title">example link</a>.</p>

语法说明:
* []里写链接文字
* ()里写链接地址, 里面的 “” 可以为链接指定title属性
* “title” 与链接地址间有空格

参考式

参考形式为链接定一个名称,之后在文件的其他地方定义该链接的内容,这种形式一般用在学术论文上面,它可以对链接进行统一方便的管理。

I get 10 times more traffic from Google than fromYahoo or MSN.

输出为HTML

<p>I get 10 times more traffic from <a href="http://google.com/" title="Google">Google</a> than from <a href="http://search.yahoo.com/" title="Yahoo Search">Yahoo</a> or <a href="http://search.msn.com/" title="MSN Search">MSN</a>.</p>

语法说明:
* 参考式链接分为两部分
* 文中的写法 [链接文字][链接标记]
* 文本的任意位置添加[链接标记]:linkaddr “title”
* 链接地址与链接标题前有一个空格
* 也可以写成[链接文字][],即链接文字本身做为链接标记
* [链接文字]:链接地址的形式,见代码的最后一行。

短链接

http://example.com/;

address@example.com;

输出为HTML

<http://example.com/>;
<address@example.com>;

语法说明:
Markdown 支持以比较简短的自动链接形式来处理网址和电子邮件信箱,只要是用<>包起来, Markdown 就会自动把它转成链接。一般网址的链接文字就和链接地址一样

锚点

网页中,锚点其实就是页内超链接,也就是链接本文档内部的某些元素,实现当前页面中的跳转。比如我这里写下一个锚点,点击回到目录,就能跳转到目录。 在目录中点击这一节,就能跳过来。还有下一节的注脚。这些根本上都是用锚点来实现的。

注意:
1. Markdown Extra 只支持在标题后插入锚点,其它地方无效。
2. Leanote 编辑器右侧显示效果区域暂时不支持锚点跳转,所以点来点去发现没有跳转不必惊慌,但是你发布成笔记或博文后是支持跳转的。

语法描述:
在你准备跳转到的指定标题后插入锚点{#标记},然后在文档的其它地方写上连接到锚点的链接。

代码:

## 0. 目录{#index}

跳转到[目录](#index)

显示效果:

跳转到目录


图片

图片的语法和链接很像,也有两种写法:行内式和参考式。

行内形式:

alt text

参考形式:

alt text

HTML输出均为:

<img src="/path/to/img.jpg" alt="alt text" title="Title" />

语法说明:
* alt 和 title 都不是必须的,可以省略,但建议写上。
* 文档要插入图片的地方![图片Alt][标记]与后面地址间要有空行或其他字符隔开


代码

在一般的段落文字中,你可以使用反引号 ` 来标记代码区段,区段内的 & 、 < 和 > 都会被自动的转换成 HTML 实体,而三个“`包围起来的区域在可以将HTML代码原样输出(前面的代码块也是这么实现的):

I strongly recommend against using any <blink> tags.

I wish SmartyPants used named entities like &mdash;
instead of decimal-encoded entites like &#8212;.

输出HTML为:

<p>I strongly recommend against using any
<code>&lt;blink&gt;</code> tags.</p>
<p>I wish SmartyPants used named entities like
<code>&amp;mdash;</code> instead of decimal-encoded
entites like <code>&amp;#8212;</code>.</p>

列表

无序列表

Markdown使用 * + - 表示无序列表。

* Red     + Red    - Red
* Green   + Green  - Green
* Blue    + Blue   - Blue

上面这三种方式其表现结果都一样

有序列表

Markdown使用数字接着一个英文句点(.)来表示有序列表。

书写顺序        展示结果为                       
1. Bird         1.Bird      
2. McHale       2.McHale    
3. Parish       3.Parish    
6. Jack         4.Jack      
5. Tom          5.Tom       
4. Jerry        6.Jerry     

列表标记上使用的数字并不会影响输出的 HTML 结果,最终还是按书写顺序来排序;
HTML输出为:

<ol>
<li>Bird</li>
<li>McHale</li>
<li>Parish</li>
<li>Jack</li>
<li>Tom</li>
<li>Jerry</li>
</ol>

列表缩进

列表项目标记通常是放在最左边,但是其实也可以缩进,最多 3 个空格,项目标记后面则一定要接着至少一个空格或制表符。
要让列表看起来更漂亮,可以把内容用固定的缩进整理好:
* Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
viverra nec, fringilla in, laoreet vitae, risus.
* Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
Suspendisse id sem consectetuer libero luctus adipiscing

这样在编写段看起来整齐多了,当然下面这这段显示也是一样的:

  • Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
    viverra nec, fringilla in, laoreet vitae, risus.
  • Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
    Suspendisse id sem consectetuer libero luctus adipiscing.

包含段落的列表

列表项目可以包含多个段落,每个项目下的段落都必须缩进 4 个空格或是 1 个制表符
1. This is a list item with two paragraphs. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit. Aliquam hendrerit
mi posuere lectus.
Vestibulum enim wisi, viverra nec, fringilla in, laoreet
vitae, risus. Donec sit amet nisl. Aliquam semper ipsum
sit amet velit.
2. Suspendisse id sem consectetuer libero luctus adipiscing

同样的不这么对编写时不这么对齐,显示时也是一样的

包含引用的列表

如果要在列表项目内放进引用,那 > 就需要缩进:

  • A list item with a blockquote:

    This is a blockquote
    inside a list item.

输出为HTML

包含引用代码区块的列表

如果要放代码区块的话,该区块就需要缩进两次,也就是 8 个空格或是 2 个制表符:

  • 一列表项包含一个列表区块:

    <代码写在这>
    

输出HTML为:

<p>这是一个普通段落:</p>
<pre><code>这是一个代码区块。
</code></pre>

特殊情况

如行首出现数字-句点-空白,这样会被认为是一个列表(实际上不是),比如:

1986. What a great season.

输出为HTML为:
1986. What a great season.

为避免这样的情况出现,可以在句点前面加上反斜杠(转义):

1986. What a great season.

分隔线

在一行中用三个以上的星号、减号、底线来建立一个分隔线,行内不能有其他东西。你也可以在星号或是减号中间插入空格。下面每种写法都可以建立分隔线:






输出为HTML

<hr>
<hr>
<hr>
<hr>
<hr>

表格

  • 使用HTML表格代码编写
  • 使用分割符: | 来分开各列; 自然行的分为行
  • 表头与表主题间以: -|- 分开
  • 分割行还可以为不同的列指定对齐方向,默认为左对齐,在-右边加上:就右对齐
学号姓名分数
小明75
小红79
小陆92
学号姓名分数
小明75
小红79
小陆92
产品价格
Leanote 高级账号60元/年
Leanote 超级账号120元/年

输出为HTML

<table>
    <thead>
        <tr>
            <th>学号</th><th>姓名</th><th>分数</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>小明</td><td></td><td>75</td>
        </tr>
        <tr>
            <td>小红</td><td></td><td>79</td>
        </tr>
        <tr>
            <td>小陆</td><td></td><td>92</td>
        </tr>
        </tbody>
        </table>

<table>
    <thead>
        <tr>
            <th>产品</th><th style="text-align:right">价格</th>
        </tr>
        </thead>
    <tbody>
        <tr>
            <td>Leanote 高级账号</td><td style="text-align:right">60元/年</td>
        </tr>
        <tr>
            <td>Leanote 超级账号</td><td style="text-align:right">120元/年</td>
        </tr>
    </tbody>
</table>

<think>好的,我现在需要帮助用户解决在React中结合TypeScript使用react-markdown预览Markdown文件的问题。首先,我需要回顾用户之前的对话和提供的引用内容,确保回答的一致性和准确性。用户之前的问题是关于如何在React中实现Markdown预览,现在他们想了解如何结合TypeScript使用react-markdown。用户还提到了几个引用,其中引用[1]提到了react-markdown-editor-lite,一个基于React的轻量编辑器;引用[2]展示了使用react-syntax-highlighter的代码块组件;引用[3]是react-markdown-editor-lite的使用教程。这些引用可能对当前问题有帮助,但用户现在关注的是react-markdown与TypeScript的结合使用。 首先,我需要确认react-markdown是否支持TypeScript。查阅官方文档,react-markdown本身是用TypeScript编写的,因此天然支持TypeScript类型。但用户可能需要类型定义的指导和示例。接下来,我需要构建一个使用TypeScript的React组件示例。基本步骤包括安装必要的依赖,如react-markdown、类型定义文件@types/react-markdown,以及语法高亮库如react-syntax-highlighter。然后,创建组件并处理类型。这里需要注意TypeScript的类型声明,例如组件的props应该定义content为string类型。在代码示例中,用户之前的问题中提到了代码高亮的部分,引用[2]中的CodeBlock组件可以作为参考。需要调整该组件以适配TypeScript,例如明确props的类型,可能使用React.ComponentProps或定义接口。另外,用户可能需要在组件中使用remark插件,如remarkGfm和rehypeRaw,这些需要正确导入并传递给ReactMarkdown组件。同时,语法高亮部分需要处理language的匹配,这里可能需要使用正则表达式来提取语言类型,并确保类型安全。安全方面,用户之前的问题提到了XSS过滤,使用rehypeRaw时需要谨慎。在TypeScript中,可能需要额外的类型断言或配置来确保安全。 样式方面,引用[2]中的github-markdown-css可以继续使用,但需要确保在TypeScript项目中正确导入CSS文件,并在组件中应用对应的类名。 最后,生成相关问题需要考虑用户可能的后续需求,例如自定义组件、数学公式支持、性能优化等,这些都需要结合之前的引用和用户的问题来设计。 总结步骤:安装依赖、创建TypeScript组件、处理类型、集成语法高亮、应用样式、注意事项和相关问题。确保每个部分都符合TypeScript的最佳实践,并参考用户提供的引用内容进行适当调整。</think>### 在 React + TypeScript 中使用 react-markdown 实现 Markdown 预览的完整指南 --- #### 一、基础集成步骤 1. **安装核心依赖** ```bash npm install react-markdown @types/react-markdown remark-gfm rehype-raw npm install react-syntax-highlighter @types/react-syntax-highlighter ``` 2. **创建 TypeScript 组件** ```tsx import React from 'react'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import rehypeRaw from 'rehype-raw'; import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism'; interface MarkdownPreviewProps { content: string; } const MarkdownPreview: React.FC<MarkdownPreviewProps> = ({ content }) => { return ( <ReactMarkdown remarkPlugins={[remarkGfm]} rehypePlugins={[rehypeRaw]} components={{ code({ node, inline, className, children, ...props }) { const match = /language-(\w+)/.exec(className || ''); return !inline && match ? ( <SyntaxHighlighter style={vscDarkPlus} language={match[1]} PreTag="div" {...props as React.ComponentProps<typeof SyntaxHighlighter>} > {String(children).replace(/\n$/, '')} </SyntaxHighlighter> ) : ( <code className={className} {...props}> {children} </code> ); } }} > {content} </ReactMarkdown> ); }; export default MarkdownPreview; ``` --- #### 二、类型处理要点 1. **Props 类型定义** ```tsx interface MarkdownPreviewProps { content: string; // 明确内容类型为字符串 className?: string; // 可选样式类 } ``` 2. **语法高亮组件类型断言** ```tsx // 处理 react-syntax-highlighter 的 TypeScript 类型 {...props as React.ComponentProps<typeof SyntaxHighlighter>} ``` --- #### 三、文件加载方案 1. **本地文件加载(通过 Webpack)** ```tsx // 需要配置 webpack 的 markdown-loader import mdContent from './document.md'; // 在组件中使用 <MarkdownPreview content={mdContent} /> ``` 2. **动态加载远程内容** ```tsx const [content, setContent] = useState(''); useEffect(() => { fetch('https://example.com/doc.md') .then(res => res.text()) .then(text => setContent(text)); }, []); ``` --- #### 四、样式优化方案 1. **应用 GitHub 风格样式** ```bash npm install github-markdown-css ``` ```tsx import 'github-markdown-css'; // 包裹组件 <div className="markdown-body"> <MarkdownPreview content={content} /> </div> ``` --- #### 五、安全注意事项 1. **HTML 内容过滤** ```tsx // 使用自定义组件过滤危险 HTML components={{ img: ({ node, ...props }) => ( <img {...props} alt={props.alt || ''} onError={(e) => e.currentTarget.style.display = 'none'} /> ) }} ``` --- ### 高级功能扩展 1. **数学公式支持** ```tsx npm install remark-math rehype-katex @types/katex ``` ```tsx import remarkMath from 'remark-math'; import rehypeKatex from 'rehype-katex'; import 'katex/dist/katex.min.css'; // 修改组件配置 remarkPlugins={[remarkGfm, remarkMath]} rehypePlugins={[rehypeRaw, rehypeKatex]} ``` --- ### 常见问题解决 1. **图片路径问题** ```tsx // 自定义图片组件处理相对路径 components={{ img: ({ src, alt }) => ( <img src={require(`../../public/${src}`)} alt={alt} style={{ maxWidth: '100%' }} /> ) }} ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值