You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>This section will explain the usual workflow of creating a new language definition.</p>
137
+
138
+
<p>As an example, we will create the language definition of the fictional <em>Foo's Binary, Artistic Robots™</em> language or just Foo Bar for short.</p>
139
+
136
140
<olclass="indent">
137
141
<li>
138
-
<p>Create a new file <code>components/prism-lang-id.js</code>.</p>
142
+
<p>Create a new file <code>components/prism-foo-bar.js</code>.</p>
143
+
144
+
<p>In this example, we choose <code>foo-bar</code> as the id of the new language. The language id has to be unique and should work well with the <code>language-xxxx</code> CSS class name Prism uses to refer to language definitions. Your language id should ideally match the regular expression <code>/^[a-z][a-z\d]*(?:-[a-z][a-z\d]*)*$/</code>.</p>
139
145
</li>
140
146
<li>
141
147
<p>Edit <code>components.json</code> to register the new language by adding it to the <code>languages</code> object. (Please note that all language entries are sorted alphabetically by title.) <br>
142
-
Your new entry will look like this:</p>
148
+
Our new entry for this example will look like this:</p>
143
149
144
-
<pre><codeclass="language-js">"lang-id": {
145
-
"title": "Language Title",
150
+
<pre><codeclass="language-js">"foo-bar": {
151
+
"title": "Foo Bar",
146
152
"owner": "Your GitHub name"
147
153
}</code></pre>
148
154
@@ -153,22 +159,58 @@ <h1>Creating a new language definition</h1>
153
159
<li>
154
160
<p>Rebuild Prism by running <codeclass="language-bash">npx gulp</code>.</p>
155
161
156
-
<p>This will make your language available to the <ahref="test.html">test page</a>, or more precise: your local version of it. You can open you local <code>test.html</code> page in any browser, select your language, and see how your language definition highlights the given code.</p>
162
+
<p>This will make your language available to the <ahref="test.html">test page</a>, or more precise: your local version of it. You can open your local <code>test.html</code> page in any browser, select your language, and see how your language definition highlights any code you input.</p>
157
163
158
-
<p><em>Note:</em> You have to reload the page to apply changes made to <code>prism-lang-id.js</code>.</p>
164
+
<p><em>Note:</em> You have to reload the test page to apply changes made to <code>prism-foo-bar.js</code>.</p>
159
165
</li>
160
166
<li>
161
167
<p>Write the language definition.</p>
162
168
163
169
<p>The <ahref="#language-definitions">above section</a> already explains the makeup of language definitions.</p>
164
170
</li>
171
+
<li>
172
+
<p>Adding aliases.</p>
173
+
174
+
<p>Aliases for are useful if your language is known under more than just one name or there are very common abbreviations for your language (e.g. JS for JavaScript). Keep in mind that aliases are very similar to language ids in that they also have to be unique (i.e. there cannot be an alias which is the same as another alias of language id) and work as CSS class names.</p>
175
+
176
+
<p>In this example, we will register the alias <code>foo</code> for <code>foo-bar</code> because Foo Bar code is stored in <code>.foo</code> files.</p>
177
+
178
+
<p>To add the alias, we add this line at the end of <code>prism-foo-bar.js</code>:</p>
<p>Aliases also have to be registered in <code>components.json</code> by adding the <code>alias</code> property to the language entry. In this example, the updated entry will look like this:</p>
183
+
184
+
<pre><codeclass="language-js">"foo-bar": {
185
+
"title": "Foo Bar",
186
+
"alias": "foo",
187
+
"owner": "Your GitHub name"
188
+
}</code></pre>
189
+
190
+
<p><em>Note:</em><code>alias</code> can also be a string array if you need to register multiple aliases.</p>
191
+
192
+
<p>Using <code>aliasTitles</code>, it's also possible to give aliases specific titles. In this example, this won't be necessary but a good example as to where this is useful is the markup language:</p>
193
+
194
+
<pre><codeclass="language-js">"markup": {
195
+
"title": "Markup",
196
+
"alias": ["html", "xml", "svg", "mathml"],
197
+
"aliasTitles": {
198
+
"html": "HTML",
199
+
"xml": "XML",
200
+
"svg": "SVG",
201
+
"mathml": "MathML"
202
+
},
203
+
"option": "default"
204
+
}</code></pre>
205
+
</li>
165
206
<li>
166
207
<p>Add some tests.</p>
167
208
168
-
<p>Create a folder <code>tests/languages/lang-id/</code>. This is where your test files will live. The test format and how to run tests is described <ahref="test-suite.html">here</a>. A good example are <ahref="https://github.com/PrismJS/prism/tree/master/tests/languages/javascript">the tests of the JavaScript language</a>.</p>
209
+
<p>Create a folder <code>tests/languages/foo-bar/</code>. This is where your test files will live. The test format and how to run tests is described <ahref="test-suite.html">here</a>.</p>
210
+
211
+
<p>You should add a test for every major feature of your language. Test files should test the common case and certain edge cases (if any). Good examples are <ahref="https://github.com/PrismJS/prism/tree/master/tests/languages/javascript">the tests of the JavaScript language</a>.</p>
169
212
170
-
<p>You should add a test for every major feature of your language. Test files should test the common case and certain edge cases (if any). <br>
171
-
You can use this template for new <code>.test</code> files:</p>
213
+
<p>You can use this template for new <code>.test</code> files:</p>
172
214
173
215
<pre><code>The code to test.
174
216
@@ -192,13 +234,13 @@ <h1>Creating a new language definition</h1>
192
234
</li>
193
235
<li>
194
236
<p>Once you <strong>carefully checked</strong> that the test case is handled correctly (i.e. by using the test page), run the following command:</p>
195
-
<codeclass="language-bash">npm run test:languages -- --language=lang-id --pretty</code>
237
+
<codeclass="language-bash">npm run test:languages -- --language=foo-bar --pretty</code>
196
238
<p>This command will check only your test files. The new test will fail because the specified JSON is incorrect but the error message of the failed test will also include the JSON of the simplified token stream Prism created. This is what we're after. Replace the current incorrect JSON with the output labeled <em>Token Stream</em>. (Please also adjust the indentation. We use tabs.)</p>
197
239
</li>
198
240
<li>
199
241
<p><strong>Carefully check</strong> that the token stream JSON you just inserted is what you expect.</p>
200
242
</li>
201
-
<li>Re-run <codeclass="language-bash">npm run test:languages -- --language=lang-id --pretty</code> to verify that the test passes.</li>
243
+
<li>Re-run <codeclass="language-bash">npm run test:languages -- --language=foo-bar --pretty</code> to verify that the test passes.</li>
202
244
</ol>
203
245
</li>
204
246
<li>
@@ -208,7 +250,7 @@ <h1>Creating a new language definition</h1>
208
250
<li>
209
251
<p>Add an example page.</p>
210
252
211
-
<p>Create a new file <code>examples/prism-lang-id.html</code>. This will be the template containing the example markup. Just look at other examples to see how these files are structured. <br>
253
+
<p>Create a new file <code>examples/prism-foo-bar.html</code>. This will be the template containing the example markup. Just look at other examples to see how these files are structured. <br>
212
254
We don't have any rules as to what counts as an example, so a single <em>Full example</em> section where you present the highlighting of the major features of the language is enough.</p>
0 commit comments