Search code examples
javascriptmarkdownmkdocs

Add script tag to markdown file


I am developing document in MKdocs and need to load some script files to my markdown file. How can I add js functions and script tags in markdown?


Solution

  • You have two options: You could include raw HTML <script> tags in your Markdown, or you could use MkDocs' extra_javascript setting to point to your own JavaScript file.

    Use raw HTML

    As the Markdown documentation explains:

    For any markup that is not covered by Markdown’s syntax, you simply use HTML itself. There’s no need to preface it or delimit it to indicate that you’re switching from Markdown to HTML; you just use the tags.

    The only restrictions are that block-level HTML elements — e.g. <div>, <table>, <pre>, <p>, etc. — must be separated from surrounding content by blank lines, and the start and end tags of the block should not be indented with tabs or spaces.

    Of course, any such scripts would be unique to the one page they are included on.

    Use extra_javascript

    If you would like a script to be available on all of your pages, MkDocs provides the extra_javascript configuration setting, which will cause a script which points to the specified file to be included on every page. Just make sure the file is available in your docs_dir.

    For example, if you create a file myscript.js, then add the following to your mkdocs.yml config file:

    extra_javascript:
        myscript.js