Skip to content

Commit

Permalink
Examples: Updated content header (PrismJS#2232)
Browse files Browse the repository at this point in the history
This changes the automatically generated header of examples on Prism's example page.

The `overrideExampleHeader` option in `components.json` is no longer supported.
  • Loading branch information
RunDevelopment authored and quentinvernot committed Sep 11, 2020
1 parent a82770f commit 6a16438
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 28 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@
},
"clike": {
"title": "C-like",
"option": "default",
"overrideExampleHeader": true
"option": "default"
},
"javascript": {
"title": "JavaScript",
Expand Down Expand Up @@ -694,7 +693,6 @@
"c",
"cpp"
],
"overrideExampleHeader": true,
"owner": "Milania1"
},
"oz": {
Expand Down
7 changes: 3 additions & 4 deletions examples/prism-clike.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<p>The C-like component is not really a language on its own,
it is the basis of many other components. To use it directly, however,
use the class <code class="language-none">"language-clike"</code>.</p>
<p><strong>Note:</strong> The C-like component is not really a language on its own,
it is the basis of many other components.</p>

<h2>Comments</h2>
<pre><code>// Single line comment
Expand All @@ -25,4 +24,4 @@ <h2>Functions</h2>
<pre><code>foo();
Bar();
_456();
</code></pre>
</code></pre>
7 changes: 3 additions & 4 deletions examples/prism-opencl.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<p>
To use this language, use the class <code class="language-none">"language-opencl"</code> for OpenCL kernel code.
Host code is automatically highlighted in <code class="language-none">"language-c"</code>
respectively <code class="language-none">"language-cpp"</code> classes.
<p><strong>Note:</strong> Use the class <code class="language-none">"language-opencl"</code> for OpenCL kernel code.
Host code is automatically highlighted with the <code class="language-none">"language-c"</code>
or <code class="language-none">"language-cpp"</code> class.
</p>

<h2>OpenCL host code</h2>
Expand Down
57 changes: 41 additions & 16 deletions scripts/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,21 @@ function getFileContents(filepath) {
}

function buildContentsHeader(id) {
function toArray(value) {
if (Array.isArray(value)) {
return value;
} else if (value != null) {
return [value];
} else {
return [];
}
}

var language = languages[id];
var header = '<h1>' + language.title + '</h1>';
if (language.overrideExampleHeader) {
return header;
}

if (language.alias) {
var alias = language.alias;
if (!Array.isArray(alias)) {
alias = [alias];
}
var alias = toArray(language.alias);

header += '<p>To use this language, use one of the following classes:</p>';
header += '<ul><li><code class="language-none">"language-' + id + '"</code></li>';
Expand All @@ -130,18 +135,38 @@ function buildContentsHeader(id) {
} else {
header += '<p>To use this language, use the class <code class="language-none">"language-' + id + '"</code>.</p>';
}

function wrapCode(text) {
return '<code class="language-none">' + text + '</code>';
}

var deps = [];
if (language.require) {
var require = language.require;
if (!Array.isArray(require)) {
require = [require];
deps.push('requires ' + toArray(language.require).map(wrapCode).join(', '));
}
if (language.optional) {
deps.push('optionally uses ' + toArray(language.optional).map(wrapCode).join(', '));
}
if (language.modify) {
deps.push('modifies ' + toArray(language.modify).map(wrapCode).join(', '));
}
if (deps.length) {
header += '<p>';
header += '<a href="https://prismjs.com/extending.html#dependencies"><strong>Dependencies:</strong></a>';
header += ' This component';
if (deps.length === 1) {
header += ' ' + deps[0] + '.';
} else {
header += ':';
header += '<ul>';
deps.forEach(function (text) {
header += '<li>' + text + '.</li>'
});
header += '</ul>';
}

header += '<p><strong>Dependencies:</strong> The following dependencies need to be loaded before this component: ';
header += require.map(function (dep) {
return '<code class="language-none">' + dep + '</code>';
}).join(', ');
header += '.</p>';
header += '</p>';
}

return header;
}

Expand Down

0 comments on commit 6a16438

Please sign in to comment.