-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code Block File Name Transformerを追加し、Astroの使用例をドキュメントに追加
- Loading branch information
Showing
4 changed files
with
133 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Astro Example | ||
|
||
`microcms-rich-editor-handler`をAstroで使用する例です。 | ||
フロントマッターでMicroCMSから取得したリッチエディタのHTMLコンテンツを処理し、目次を生成して表示します。 | ||
|
||
```astro {25-33} | ||
--- | ||
import type { MicroCMSListContent } from "microcms-js-sdk"; | ||
import { createClient } from "microcms-js-sdk"; | ||
import { | ||
microCMSRichEditorHandler, | ||
responsiveImageTransformer, | ||
tocExtractor, | ||
} from "microcms-rich-editor-handler"; | ||
const client = createClient({ | ||
serviceDomain: import.meta.env.MICROCMS_SERVICE_DOMAIN, | ||
apiKey: import.meta.env.MICROCMS_API_KEY, | ||
}); | ||
export type Blog = { | ||
title: string; | ||
content: string; | ||
} & MicroCMSListContent; | ||
const blogContent = await client.getListDetail<Blog>({ | ||
endpoint: "blogs", | ||
contentId: "id001", | ||
}); | ||
const { | ||
html, | ||
data: { toc }, | ||
} = await microCMSRichEditorHandler(blogContent.content, { | ||
transformers: [responsiveImageTransformer()], | ||
extractors: { | ||
toc: [tocExtractor(), { phase: "before" }], | ||
}, | ||
}); | ||
blogContent.content = html; | ||
--- | ||
<main> | ||
<nav> | ||
{toc.map((item) => ( | ||
<a href={`#${item.id}`}>{item.text}</a> | ||
))} | ||
</nav> | ||
<section> | ||
<h1>{blogContent.title}</h1> | ||
<div set:html={blogContent.content}></div> | ||
</section> | ||
</main> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Code Block File Name Transformer | ||
|
||
`Code Block File Name` は、コードブロックのファイル名を表示するためのTransformerです。 | ||
microCMSではコードブロックにファイル名を指定することができ、そのファイル名を表示するために使用します。 | ||
|
||
## Type Definition | ||
|
||
```ts | ||
import type { Transformer } from "./types"; | ||
|
||
type Options = { | ||
/** | ||
* ファイル名を表示する要素のタグ名 | ||
* デフォルトは span | ||
*/ | ||
tagName?: string | undefined | ||
/** | ||
* ファイル名を表示する要素に追加する属性 | ||
*/ | ||
attributes?: Record<string, string> | undefined; | ||
}; | ||
|
||
/** | ||
* コードブロックのファイル名を表示する | ||
*/ | ||
const codeBlockFileNameTransformer: (options?: Options | undefined) => Transformer | ||
``` | ||
## Usage | ||
```ts | ||
import { microCMSRichEditorHandler, codeBlockFileNameTransformer } from "microcms-rich-editor-handler"; | ||
|
||
const html = ` | ||
<div data-filename="index.js"> | ||
<pre><code>console.log("Hello, World!")</code></pre> | ||
</div> | ||
`; | ||
|
||
const main = async () => { | ||
const { html: transformedHtml } = await microCMSRichEditorHandler({ | ||
transformers: [codeBlockFileNameTransformer()], | ||
}); | ||
|
||
console.log(transformedHtml); | ||
}; | ||
|
||
main(); | ||
``` | ||
|
||
Output: | ||
|
||
```html | ||
<div data-filename="index.js"> | ||
<span>index.js</span> | ||
<pre><code>console.log("Hello, World!")</code></pre> | ||
</div> | ||
``` |