Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

コードブロックのテスト #6

Open
takejohn opened this issue Dec 4, 2024 · 2 comments
Open

コードブロックのテスト #6

takejohn opened this issue Dec 4, 2024 · 2 comments
Assignees

Comments

@takejohn
Copy link
Contributor

takejohn commented Dec 4, 2024

CIでもチェックしたい

@FineArchs
Copy link
Member

MarkdownのASTを参照するのが楽そうですね
vitepressが使っているmarkdown-itを使うなら、MarkdownIt.prototype.parseでよさそう?(ASTのドキュメントが無いですが)

@uzmoi
Copy link
Member

uzmoi commented Dec 5, 2024

vitepressが独自記法のプラグインなどを含んだmarkdown-itのインスタンスを返す関数をexportしているようなので、これが使えますね。
fenceがコードブロックで1.infoに言語名(0行目?)が入るようです。

import { createMarkdownRenderer } from "vitepress";

const md = await createMarkdownRenderer();
const tokens = md.parse(markdownText, {});
const codes = tokens
  .filter(token =>
      token.type === "fence" &&
        (token.info === "ais" || token.info === "aiscript"))
  .map(token => token.content);

Footnotes

  1. https://spec.commonmark.org/0.31.2/#fenced-code-blocks

@takejohn takejohn self-assigned this Dec 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants