Markdown语法示例

Wuzi

January 2020, 1

4 min read

Markdown语法示例

Aside 提示组件

依次展示为:代码 - 效果。

note

:::note[这是标题]
这是一个很长的描述信息。
:::

tip

:::tip[这是标题]
这是一个很长的描述信息。
:::

danger

:::danger[这是标题]
这是一个很长的描述信息。
:::

caution

:::caution[这是标题]
这是一个很长的描述信息。
:::

代码块

给代码块添加标题

代码

\`\`\`ts title=hello.ts
console.log("Hello world!");
\`\`\`

效果

hello.ts
console.log("Hello world!");

高亮某些文字

代码

\`\`\`ts "console"
console.log("Hello");
console.log("world!");
\`\`\`

效果

console.log("Hello world!");
console.log("world!");

高亮某些行

代码

\`\`\`ts {3, 5-7}
import { MarkdownInstance } from "astro";
export const formatDate = (pubDate: string) => {
var options: Intl.DateTimeFormatOptions = {
weekday: 'short',
year: 'numeric',
month: 'long',
day: 'numeric'
};
return new Date(pubDate).toLocaleDateString('en-US', options);
}
export const sortPostsByDate = (a: MarkdownInstance<any>, b: MarkdownInstance<any>) => {
const pubDateA = new Date(a.frontmatter.pubDate);
const pubDateB = new Date(b.frontmatter.pubDate);
if (pubDateA < pubDateB) {
return 1;
}
if(pubDateA > pubDateB) {
return -1;
}
return 0;
}
\`\`\`

效果

import { MarkdownInstance } from "astro";
export const formatDate = (pubDate: string) => {
var options: Intl.DateTimeFormatOptions = {
weekday: 'short',
year: 'numeric',
month: 'long',
day: 'numeric'
};
return new Date(pubDate).toLocaleDateString('en-US', options);
}
export const sortPostsByDate = (a: MarkdownInstance<any>, b: MarkdownInstance<any>) => {
const pubDateA = new Date(a.frontmatter.pubDate);
const pubDateB = new Date(b.frontmatter.pubDate);
if (pubDateA < pubDateB) {
return 1;
}
if(pubDateA > pubDateB) {
return -1;
}
return 0;
}

新增或删除

代码

\`\`\`ts ins={3, 5-7} del={6-8, 17-22}
import { MarkdownInstance } from "astro";
export const formatDate = (pubDate: string) => {
var options: Intl.DateTimeFormatOptions = {
weekday: 'short',
year: 'numeric',
month: 'long',
day: 'numeric'
};
return new Date(pubDate).toLocaleDateString('en-US', options);
}
export const sortPostsByDate = (a: MarkdownInstance<any>, b: MarkdownInstance<any>) => {
const pubDateA = new Date(a.frontmatter.pubDate);
const pubDateB = new Date(b.frontmatter.pubDate);
if (pubDateA < pubDateB) {
return 1;
}
if(pubDateA > pubDateB) {
return -1;
}
return 0;
}
\`\`\`

效果

import { MarkdownInstance } from "astro";
export const formatDate = (pubDate: string) => {
var options: Intl.DateTimeFormatOptions = {
weekday: 'short',
year: 'numeric',
month: 'long',
day: 'numeric'
};
return new Date(pubDate).toLocaleDateString('en-US', options);
}
export const sortPostsByDate = (a: MarkdownInstance<any>, b: MarkdownInstance<any>) => {
const pubDateA = new Date(a.frontmatter.pubDate);
const pubDateB = new Date(b.frontmatter.pubDate);
if (pubDateA < pubDateB) {
return 1;
}
if(pubDateA > pubDateB) {
return -1;
}
return 0;
}

显示行号

代码

\`\`\`ts showLineNumbers
console.log("Hello");
console.log("world!");
\`\`\`

效果

console.log("Hello world!");
console.log("world!");

折叠某些行

代码

\`\`\`ts collapse={4-6}
export const sortPostsByDate = (a: MarkdownInstance<any>, b: MarkdownInstance<any>) => {
const pubDateA = new Date(a.frontmatter.pubDate);
const pubDateB = new Date(b.frontmatter.pubDate);
if (pubDateA < pubDateB) {
return 1;
}
if(pubDateA > pubDateB) {
return -1;
}
return 0;
}
\`\`\`

效果

export const sortPostsByDate = (a: MarkdownInstance<any>, b: MarkdownInstance<any>) => {
const pubDateA = new Date(a.frontmatter.pubDate);
const pubDateB = new Date(b.frontmatter.pubDate);
3 collapsed lines
if (pubDateA < pubDateB) {
return 1;
}
if(pubDateA > pubDateB) {
return -1;
}
return 0;
}

案例汇总

file.tsx
const binaryStringToArray = (binaryString: string | undefined) => {
5 collapsed lines
if (!binaryString) {
return [];
}
return binaryString.split("").map((char) => parseInt(char, 10));
};
const IChingIcon = ({ base, value }: IconProps) => {
const valueArray = binaryStringToArray(value);
return (
<svg
key={value}
fill="none"
height={base ? "24" : "48"}
viewBox={base ? "0 0 24 24" : "0 0 24 48"}
width="24"
xmlns="http://www.w3.org/2000/svg"
>
{valueArray.map((value, index) => {
if (value === 0) {
return (
<React.Fragment key={index}>
<rect
key={`left-${index}`}
fill="currentColor"
height="4"
width="10"
x="0"
y={8 * index + 2}
/>
<rect
key={`right-${index}`}
fill="currentColor"
height="4"
width="10"
x="14"
y={8 * index + 2}
/>
</React.Fragment>
);
} else {
return (
<rect
key={index}
fill="#FF0000"
height="4"
width="24"
x="0"
y={8 * index + 2}
/>
);
}
})}
</svg>
);
};
Share to X