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

custom syntax for code block #138

Closed
ZigZagT opened this issue Oct 8, 2016 · 21 comments
Closed

custom syntax for code block #138

ZigZagT opened this issue Oct 8, 2016 · 21 comments

Comments

@ZigZagT
Copy link

ZigZagT commented Oct 8, 2016

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
Copy link
Owner

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
Copy link
Author

ZigZagT commented Oct 8, 2016

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

@ZigZagT ZigZagT closed this as completed Oct 8, 2016
@burodepeper
Copy link
Owner

You're welcome.

@ZigZagT
Copy link
Author

ZigZagT commented Oct 8, 2016

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
Copy link
Owner

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.

@burodepeper burodepeper reopened this Oct 8, 2016
@ZigZagT
Copy link
Author

ZigZagT commented Oct 8, 2016

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

ZigZagT commented Oct 8, 2016

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

@burodepeper
Copy link
Owner

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

@ZigZagT
Copy link
Author

ZigZagT commented Oct 9, 2016

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

  • The language-markdown package works with the {javascript } code block, but I have to insert a whitespace between javascript and }. It seems a little bug.
  • In R markdown, in the '''{r } '''(for convenience, I'm using ' to replace in this comment) pattern, the prefix{rshould be a keyword indicates that the R markdown parser should treat this "code block" as an "code chunk". The language used inside the chunk should be specified by theengineoption. For example,'''{r engine="javascript"} '''indicates that this code chunk contains javascript code. This is doced [here](http://yihui.name/knitr/demo/engines/). Any code chunk beginning label replaces therin'''{r } '''with other language name, should be an shorthand or theengineoption. doced [here](http://rmarkdown.rstudio.com/authoring_knitr_engines.html). Language-markdown package seems not supports theengine` option grammar.

@burodepeper
Copy link
Owner

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
Copy link
Author

ZigZagT commented Oct 9, 2016

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

@burodepeper
Copy link
Owner

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
Copy link
Author

ZigZagT commented Oct 9, 2016

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
Copy link
Owner

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
Copy link
Author

ZigZagT commented Oct 9, 2016

That's fine, thank you

@Edenharder
Copy link

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

@burodepeper
Copy link
Owner

@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
Copy link

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
Copy link
Owner

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.

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