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

Document how to create code blocks with syntax highlighting #917

Closed
Onnno opened this issue Dec 14, 2018 · 10 comments
Closed

Document how to create code blocks with syntax highlighting #917

Onnno opened this issue Dec 14, 2018 · 10 comments
Labels
question Question about functionality

Comments

@Onnno
Copy link

Onnno commented Dec 14, 2018

I use TypeDoc successfully now, but I can't get Highlight.js make my example code being highlighted.
I installed TypeDoc as a local development package (--save-dev) and I think it installed highlight.js automatically. However, because there was no highlighting happening, I updated highlight.js.
I don't really know why things don't work.
I used some example Code in a .ts code file comment (/** I put example code like code you can see below here */) as well as in my introductory .md markup file.
Nothing works, also not with a class indicator, neither Typescript, typescript, TypeScript or ts.
My example code simply was something like:
myExampleFunc(n: a.INotification): string {
this.ExampleResource.markUsed({ id: n.Id });
return "OK";
}

Can anyone help me get this running?

@jonchardy
Copy link
Contributor

What do your docstrings look like? Highlighting works for us using this style:

/**
 * Here is a docstring that contains some code...
 *
 * Code example:
 * ```js
 * function foo() {
 *   console.log('Welcome to foo');
 * }
 * ```
 */
public methodA() {
  // ...
}

@aciccarello
Copy link
Collaborator

Would it be better if TypeDoc defaulted code to TypeScript?

@Onnno
Copy link
Author

Onnno commented Dec 17, 2018

@jonchardy Thanks a lot. Works now.
I couldn't find that ```-syntax on the web.

Instead, I found this:

This will find and highlight code inside of <pre><code> tags;
it tries to detect the language automatically.
If automatic detection doesn’t work for you,
you can specify the language in the class attribute:
<pre><code class="typescript">...</code></pre>

on https://highlightjs.org/usage/

@aciccarello: Now that I know the syntax I can mark code as Typescript - however, of course it makes sense to default typedoc to typescript.

@aciccarello aciccarello added enhancement Improved functionality question Question about functionality docs labels Dec 17, 2018
@9oelM
Copy link
Contributor

9oelM commented Dec 19, 2018

why would highlightjs not automatically detect the language here?

typedoc/src/lib/output/plugins/MarkedPlugin.ts

/**
     * Highlight the synatx of the given text using HighlightJS.
     *
     * @param text  The text taht should be highlightes.
     * @param lang  The language that should be used to highlight the string.
     * @return A html string with syntax highlighting.
     */
    public getHighlighted(text: string, lang?: string): string {
        try {
            if (lang) {
                return HighlightJS.highlight(lang, text).value;
            } else {
                return HighlightJS.highlightAuto(text).value;
            }
        } catch (error) {
            this.application.logger.warn(error.message);
            return text;
        }
    }

anyhow, if we wanted to default the language to typescript, we could just set the default param (lang) to "typescript" here and make some few lines of changes accordingly?

@Onnno
Copy link
Author

Onnno commented Dec 19, 2018

Yes, works without 'ts'. Thanks. No need for default param.
Also found that triple accent syntax now :-).
Thanks for help and sorry for some maybe simple questions. I'm new to this :-O

@Onnno Onnno closed this as completed Dec 19, 2018
@aciccarello
Copy link
Collaborator

That's true, it should be autodetecting the language. We can add a note about code fences and highlightjs to the doccomments documentation.

@aciccarello aciccarello reopened this Dec 19, 2018
@aciccarello aciccarello removed the enhancement Improved functionality label Dec 19, 2018
@9oelM
Copy link
Contributor

9oelM commented Dec 19, 2018

is typedoc-site the right place to submit a PR on that?

@Onnno
Copy link
Author

Onnno commented Dec 20, 2018

TypeDoc's howto document is here: TypeDoc and links to HighlightJS on github.
So people may expect an explanation rather on one of these pages I guess.
I'd prefer to see it in github HighlightJS page.

@aciccarello
Copy link
Collaborator

@9oelM I'm working on some changes to the website infrastructure so I'll make this change myself. Thanks!

@aciccarello aciccarello changed the title HighLight.js does not highlight code at all Document how to create code blocks with syntax highlighting Dec 20, 2018
@9oelM
Copy link
Contributor

9oelM commented Dec 21, 2018

@aciccarello great! looking forward to seeing the website also getting optimized for mobile view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question about functionality
Projects
None yet
Development

No branches or pull requests

4 participants