burodepeper / language-markdown

Add support for Markdown to Atom (including Github flavored, Markdown Extra, CriticMark, YAML/TOML front-matter, and R Markdown), and smart behavior to lists.
https://atom.io/packages/language-markdown
MIT License
119 stars 296 forks source link

custom syntax for code block #138

Closed ZigZagT closed 8 years ago

ZigZagT commented 8 years ago

I'm trying making something like this

```ttt
some code without syntax highlight
```

and want the code to be highlighted just like well-known language syntax

```javascript
some code with syntax highlight
```

How can i do it?

burodepeper commented 8 years ago

This ttt you are speaking of, is that just a placeholder, or is it an already existing language?

How this works in either way is that there's a list of recognized languages for the fenced-code-blocks (see here) and if ttt is an existing language, which has an existing grammar-package (for instance, language-javascript would be embedded in your second example), it is fairly simple to add it to that list, and from then on the code will be highlighted in your Markdown document.

However, if ttt doesn't have a grammar yet, you'll have to create one before you (or anybody else) can add it to the list. Writing a grammar from scratch is no easy task though.

Does this help?

ZigZagT commented 8 years ago

I'm using it just as an alias of another language. The fenced-code-blocks is what I need. Thank you very much.

burodepeper commented 8 years ago

You're welcome.

ZigZagT commented 8 years ago

I just tried that, but it not work at all. I'm not aware about coffee script as well as atom. I'm sorry to troubled you again and again, but please let me know where I went wrong.

cd into /.atom/packages/language-markdown, and modify the /grammars/fixtures/fenced-code.cson file:

from:

 { pattern:'javascript|js|jsx', include:'source.js' }

to:

 { pattern:'javascript|js|jsx|ttt', include:'source.js' }

restart atom

burodepeper commented 8 years ago

Not a problem.

There are some instructions in the README on how the grammar of this package is compiled. The thing with your approach though, is that it will break every time language-markdown gets updated.

Is your ttt thing something that would benefit others? If so, I can add it to the main package. That would be the easiest solution for you.

ZigZagT commented 8 years ago

ttt is just a placeholder. I'm trying to use pure markdown to finish some complex job, for example, build an non-static blog site. So I have to extends the functionality of original markdown. The most suitable appoach is to use custom code block to represent extended fetures. In this case, I want to use

```run
some javascript code with syntax highlight
```

to indicate that this piece of code should will run in the browser through my custom markdown parser, while it also visible in other standard markdown parser. Recompile the package every time adding an alias (like ttt or run) should not a good idea.

burodepeper commented 8 years ago

I think I know a better already available solution. There's support for, darn it, I forgot the name, something like custom attributes, or special attributes or something. It would look something like this:

```javascript {run}
alert("BadaBing");
```

If you could adapt your custom markdown parser to recognize this pattern (the curly braces notation is often used, in Extra Markdown and R Markdown for instance) then I believe you're all set. It will still be recognized and highlighted as Javascript, and you needn't worry about updates.

burodepeper commented 8 years ago

Have a look at this spec to get an idea of what else the notation is used for. You could easily extend your own parser with additional functionality if needed.

ZigZagT commented 8 years ago

It look good. I'm trying to make it work.

burodepeper commented 8 years ago

Let me know how it works out. Bed time over here, so I'll check in again tomorrow. Good luck!

ZigZagT commented 8 years ago

I had checked the r markdown specification and had learn about the knitr engine. And I found these problems:

burodepeper commented 8 years ago

R Markdown support is indeed still incomplete, see #93. I've added the trailing space as a bug there, but I have to admit that priority to fully implement R Markdown is not high. A big part of that is the engine argument you found; I want to avoid having to generate hundreds of lines of code (which would slow down the parser) that only a handful of users use.

Are you thinking of dropping your custom parser in favor of R Markdown? It is an interesting superset of Markdown, but there's a good chance that syntax highlighting within R Markdown blocks won't happen. Can you make your custom parser work with the ```javascript {run} syntax I suggested?

ZigZagT commented 8 years ago

I had add basic r markdown support for marked, a parser in javascript.

burodepeper commented 8 years ago

If you have to add it yourself, I still suggest going with the ```javascript {run} I suggested. The curly braces are more or less an accepted addition to the Markdown syntax, and besides fully supporting R Markdown (which is an investment I'm not willing to make just for this issue) I'm not sure there's anything else I can do for you.

ZigZagT commented 8 years ago

The engin argument is not important in markdown, the {js xxx}</code> syntax is enough for me. The <code>javascript {run} is a little far away of the "standard", i'm trying to avoid this.

burodepeper commented 8 years ago

Well, since ```{js xxx} works with syntax-highlighting, I think we can close this issue then. Let me know if you run into any other issues.

ZigZagT commented 8 years ago

That's fine, thank you

Edenharder commented 8 years ago

Where can I find the source.languagename file if I have already installed the language grammer?

burodepeper commented 8 years ago

@Edenharder I'm not sure what you are looking for. Could you explain it in a bit more detail? Also, if it is unrelated to this issue, please create a new one.

Edenharder commented 8 years ago

In the file fenced-code.md, it says that

# Objects in {list} have the following structure:
#
# {
#   pattern: {string}
#   include: {string}
#   contentName: {string}
# }
#
# 'pattern' is required; item is ignored if 'pattern' is missing
# If 'include' is omitted, 'include' will become 'source.'+pattern
# If 'contentName' is omitted, 'contentName' will become 'embedded.'+pattern

If I want to add support for a new language, for example, latex. And I have installed the package language-latex already. Then I need to find out where 'source.latex' is to configure fenced-code.md. But I do not know where it will be usually.

burodepeper commented 8 years ago

That depends on the language ; )

The source.languagename you are referring too is the base scope that these grammars use. Atom is essentially a webpage, and this base scope is the css class given to a line that contains code from that particular language. The thing is, it differs for a number of languages.

The easiest way to find this scope is to open the main grammar file for a language (grammars/language-markdown.json in the case of this package) and (usually) near the top of the file you'll see an item named scopeName, and that's the value you are looking for that you'll need for the include section of the fenced-code bit. The scopeName for language-latex would be text.tex.latex.

Latex will be added in an upcoming version though. I've recently added support for inline math, and it's part of that. If you need help with any other languages, feel free to open a new issue, point me to the language-packages, and it should be in the next version.