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

How should I get backtick count of fenced code block? #461

Closed
yumetodo opened this issue Aug 8, 2020 · 1 comment
Closed

How should I get backtick count of fenced code block? #461

yumetodo opened this issue Aug 8, 2020 · 1 comment

Comments

@yumetodo
Copy link

yumetodo commented Aug 8, 2020

According to the CommonMark spec, fenced code block can be nested like below:

`````markdown
````markdown
```typescript
console.log("arikitari na sekai");
```
````
`````

To build markdown from pulldown-cmark's event system like pulldown-cmark-to-cmark, backtick count information is required(In the above example, backtick count is 5). Without the information, we have no choice but to use the hard-coded value.

However, currently, the Fenced event doesn't provide the information.
https://docs.rs/pulldown-cmark/0.7.2/pulldown_cmark/enum.CodeBlockKind.html#variant.Fenced

$cat sample.md
`````markdown
````markdown
```typescript
console.log("arikitari na sekai");
```
````
`````

$cat sample.md | pulldown-cmark -e
0..90: Start(CodeBlock(Fenced(Borrowed("markdown"))))
14..85: Text(Borrowed("````markdown\n```typescript\nconsole.log(\"arikitari na sekai\");\n```\n````\n"))
0..90: End(CodeBlock(Fenced(Borrowed("markdown"))))
EOF

How should I get it?

ref:

@marcusklaas
Copy link
Collaborator

Hi. Apologies for the delay in responding to this.

You may have figured this out yourself by now, but if you haven't, this may be one way to do it: take the starting byte of the fenced code block and use that as an index into the source text. You can then slice the text starting from that byte and count the number of backticks.

if let (Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(..))), range) = event {
    let num_backticks = source_text[range].chars().take_while(|c| c == '`').count();
}

Hope this works for you!

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

2 participants