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

Markdown: Added support for auto-loading code block languages #1898

Merged
merged 7 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions components/prism-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,23 @@
var grammar = Prism.languages[codeLang];

if (!grammar) {
return;
}
if (codeLang && codeLang !== 'none' && Prism.plugins.autoloader) {
var id = 'md-' + new Date().valueOf() + '-' + Math.floor(Math.random() * 1e16);
env.attributes['id'] = id;

// reverse Prism.util.encode
var code = env.content.replace(/&lt;/g, '<').replace(/&amp;/g, '&');
Prism.plugins.autoloader.loadLanguages(codeLang, function () {
var ele = document.getElementById(id);
if (ele) {
ele.innerHTML = Prism.highlight(ele.textContent, Prism.languages[codeLang], codeLang);
}
});
}
} else {
// reverse Prism.util.encode
var code = env.content.replace(/&lt;/g, '<').replace(/&amp;/g, '&');

env.content = Prism.highlight(code, grammar, codeLang);
env.content = Prism.highlight(code, grammar, codeLang);
}
});

Prism.languages.md = Prism.languages.markdown;
Expand Down
2 changes: 1 addition & 1 deletion components/prism-markdown.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions plugins/autoloader/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ <h1>Examples</h1>

</section>

<section>
<h1>Markdown</h1>

<p>Markdown will use the Autoloader to automatically load missing languages.</p>
<pre><code class="language-markdown">The C# code will be highlighted __after__ the rest of this document.

```csharp
public class Foo : IBar&lt;int&gt; {
public string Baz { get; set; } = "foo";
}
```

The CSS code will be highlighted with this document because CSS has already been loaded.

```css
a:hover {
color: green !important;
}
```</code></pre>

</section>

<footer data-src="templates/footer.html" data-type="text/html"></footer>

<script src="components/prism-core.js"></script>
Expand Down
25 changes: 23 additions & 2 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,28 @@
}
var config = Prism.plugins.autoloader = {
languages_path: languages_path,
use_minified: true
use_minified: true,
/**
* Loads the given language(s) and calls the given callback(s) asynchronously.
*
* The error callback will be called once for each language failing to load.
*
* @param {string|string[]} languages
* @param {(languages: string[]) => void} [success]
* @param {(language: string) => void} [error]
*/
loadLanguages: function (languages, success, error) {
// loadLanguages might call the callbacks synchronously, so we guarantee that this is not the case
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
loadLanguages(languages, function (langs) {
setTimeout(function () {
success && success(langs);
});
}, function (lang) {
setTimeout(function () {
error && error(lang);
});
});
}
};

/**
Expand Down Expand Up @@ -318,7 +339,7 @@

var dependencies = lang_dependencies[lang];
if(dependencies && dependencies.length) {
loadLanguages(dependencies, load);
loadLanguages(dependencies, load, error);
} else {
load();
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.