Skip to content

Commit 8dd563a

Browse files
committed
Code Block File Name Transformerを追加し、Astroの使用例をドキュメントに追加
1 parent 5e9b68b commit 8dd563a

File tree

4 files changed

+133
-48
lines changed

4 files changed

+133
-48
lines changed

docs/.vitepress/config.mts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export default defineConfig({
3838
text: "Syntax Highlighting by Shiki",
3939
link: "/features/transformer/syntax-highlighting-by-shiki",
4040
},
41+
{
42+
text: "Code Block File Name",
43+
link: "/features/transformer/code-block-file-name",
44+
},
4145
{
4246
text: "Attributes",
4347
link: "/features/transformer/attributes",
@@ -61,6 +65,7 @@ export default defineConfig({
6165
items: [
6266
{ text: "Responsive Image", link: "/examples/responsive-image" },
6367
{ text: "Lazy Loading", link: "/examples/lazy-loading" },
68+
{ text: "Astro", link: "/examples/astro" },
6469
],
6570
},
6671
],

docs/examples/astro.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Astro Example
2+
3+
`microcms-rich-editor-handler`をAstroで使用する例です。
4+
フロントマッターでMicroCMSから取得したリッチエディタのHTMLコンテンツを処理し、目次を生成して表示します。
5+
6+
```astro {25-33}
7+
---
8+
import type { MicroCMSListContent } from "microcms-js-sdk";
9+
import { createClient } from "microcms-js-sdk";
10+
import {
11+
microCMSRichEditorHandler,
12+
responsiveImageTransformer,
13+
tocExtractor,
14+
} from "microcms-rich-editor-handler";
15+
16+
const client = createClient({
17+
serviceDomain: import.meta.env.MICROCMS_SERVICE_DOMAIN,
18+
apiKey: import.meta.env.MICROCMS_API_KEY,
19+
});
20+
21+
export type Blog = {
22+
title: string;
23+
content: string;
24+
} & MicroCMSListContent;
25+
26+
const blogContent = await client.getListDetail<Blog>({
27+
endpoint: "blogs",
28+
contentId: "id001",
29+
});
30+
31+
const {
32+
html,
33+
data: { toc },
34+
} = await microCMSRichEditorHandler(blogContent.content, {
35+
transformers: [responsiveImageTransformer()],
36+
extractors: {
37+
toc: [tocExtractor(), { phase: "before" }],
38+
},
39+
});
40+
41+
blogContent.content = html;
42+
---
43+
44+
<main>
45+
<nav>
46+
{toc.map((item) => (
47+
<a href={`#${item.id}`}>{item.text}</a>
48+
))}
49+
</nav>
50+
<section>
51+
<h1>{blogContent.title}</h1>
52+
<div set:html={blogContent.content}></div>
53+
</section>
54+
</main>
55+
```

docs/features/handler.md

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,57 +39,24 @@ export const microCMSRichEditorHandler = async <T extends DefaultGenerics>(
3939

4040
extractorsに指定したキーに対応するするようにdataの型を決定するために少し複雑になっていますが、基本的には以下のように使います。
4141

42-
例)Astroでの使用例
43-
44-
```astro {25-33}
45-
---
46-
import type { MicroCMSListContent } from "microcms-js-sdk";
47-
import { createClient } from "microcms-js-sdk";
42+
```js
4843
import {
4944
microCMSRichEditorHandler,
5045
responsiveImageTransformer,
51-
tocExtractor,
52-
} from "microcms-rich-editor-handler";
53-
54-
const client = createClient({
55-
serviceDomain: import.meta.env.MICROCMS_SERVICE_DOMAIN,
56-
apiKey: import.meta.env.MICROCMS_API_KEY,
57-
});
58-
59-
export type Blog = {
60-
title: string;
61-
content: string;
62-
} & MicroCMSListContent;
63-
64-
const blogContent = await client.getListDetail<Blog>({
65-
endpoint: "blogs",
66-
contentId: "id001",
67-
});
68-
69-
const {
70-
html,
71-
data: { toc },
72-
} = await microCMSRichEditorHandler(blogContent.content, {
73-
transformers: [responsiveImageTransformer()],
74-
extractors: {
75-
toc: [tocExtractor(), { phase: "before" }],
76-
},
77-
});
78-
79-
blogContent.content = html;
80-
---
81-
82-
<main>
83-
<nav>
84-
{toc.map((item) => (
85-
<a href={`#${item.id}`}>{item.text}</a>
86-
))}
87-
</nav>
88-
<section>
89-
<h1>{blogContent.title}</h1>
90-
<div set:html={blogContent.content}></div>
91-
</section>
92-
</main>
46+
tocExtractor
47+
} from 'microcms-rich-editor-handler';
48+
49+
const main = async () => {
50+
const { html, data } = await microCMSRichEditorHandler(
51+
dataFromMicroCMS, // MicroCMSから取得したデータのリッチエディタのHTML文字列
52+
{
53+
transformers: [responsiveImageTransformer()],
54+
extractors: {
55+
toc: [tocExtractor(), { phase: "before" }],
56+
},
57+
},
58+
);
59+
};
9360
```
9461

9562
## Parameters
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Code Block File Name Transformer
2+
3+
`Code Block File Name` は、コードブロックのファイル名を表示するためのTransformerです。
4+
microCMSではコードブロックにファイル名を指定することができ、そのファイル名を表示するために使用します。
5+
6+
## Type Definition
7+
8+
```ts
9+
import type { Transformer } from "./types";
10+
11+
type Options = {
12+
/**
13+
* ファイル名を表示する要素のタグ名
14+
* デフォルトは span
15+
*/
16+
tagName?: string | undefined
17+
/**
18+
* ファイル名を表示する要素に追加する属性
19+
*/
20+
attributes?: Record<string, string> | undefined;
21+
};
22+
23+
/**
24+
* コードブロックのファイル名を表示する
25+
*/
26+
const codeBlockFileNameTransformer: (options?: Options | undefined) => Transformer
27+
```
28+
29+
## Usage
30+
31+
```ts
32+
import { microCMSRichEditorHandler, codeBlockFileNameTransformer } from "microcms-rich-editor-handler";
33+
34+
const html = `
35+
<div data-filename="index.js">
36+
<pre><code>console.log("Hello, World!")</code></pre>
37+
</div>
38+
`;
39+
40+
const main = async () => {
41+
const { html: transformedHtml } = await microCMSRichEditorHandler({
42+
transformers: [codeBlockFileNameTransformer()],
43+
});
44+
45+
console.log(transformedHtml);
46+
};
47+
48+
main();
49+
```
50+
51+
Output:
52+
53+
```html
54+
<div data-filename="index.js">
55+
<span>index.js</span>
56+
<pre><code>console.log("Hello, World!")</code></pre>
57+
</div>
58+
```

0 commit comments

Comments
 (0)