Skip to content

Commit e4089ad

Browse files
committed
Add support for <pre> to code
1 parent 25af221 commit e4089ad

File tree

7 files changed

+96
-1
lines changed

7 files changed

+96
-1
lines changed

handlers/code.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
module.exports = code;
4+
5+
var is = require('hast-util-is-element');
6+
var has = require('hast-util-has-property');
7+
var toString = require('hast-util-to-string');
8+
9+
var prefix = 'language-';
10+
11+
function code(h, node) {
12+
var values = node.children;
13+
var length = values.length;
14+
var index = -1;
15+
var value;
16+
var classList;
17+
var lang;
18+
19+
while (++index < length) {
20+
value = values[index];
21+
if (is(value, 'code') && has(value, 'className')) {
22+
classList = value.properties.className;
23+
break;
24+
}
25+
}
26+
27+
if (classList) {
28+
length = classList.length;
29+
index = -1;
30+
31+
while (++index < length) {
32+
value = classList[index];
33+
34+
if (value.slice(0, prefix.length) === prefix) {
35+
lang = value.slice(prefix.length);
36+
break;
37+
}
38+
}
39+
}
40+
41+
return h(node, 'code', {lang: lang || null}, toString(node));
42+
}

handlers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exports.code = exports.kbd = exports.samp = exports.var = require('./inline-code
1010
exports.img = require('./image');
1111

1212
exports.p = require('./paragraph');
13+
exports.pre = require('./code');
1314

1415
exports.h1 = exports.h2 = exports.h3 =
1516
exports.h4 = exports.h5 = exports.h6 = require('./heading');

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"homepage": "https://github.com/syntax-tree/hast-util-to-mdast#readme",
2424
"dependencies": {
2525
"has": "^1.0.1",
26+
"hast-util-has-property": "^1.0.0",
27+
"hast-util-is-element": "^1.0.0",
2628
"hast-util-to-string": "^1.0.0",
2729
"trim-lines": "^1.1.0",
2830
"unist-builder": "^1.0.2",

tests/fixtures/pre/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<pre>alpha();</pre>
2+
<pre><code>bravo();</code></pre>
3+
<pre><code class="language-js">charlie();</code></pre>
4+
<pre><code class="delta language-js echo">foxtrot();</code></pre>
5+
<pre>golf <code>hotel();</code> india</pre>
6+
<pre>juliet <code>kilo();</code></pre>
7+
<pre><code>lima();</code> mike</pre>
8+
<pre><code></code></pre>
9+
<pre>november <div><code>oscar();</code></div> papa</pre>

tests/fixtures/pre/index.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"fragment": true,
3+
"stringify": false
4+
}

tests/fixtures/pre/index.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
```
2+
alpha();
3+
```
4+
5+
```
6+
bravo();
7+
```
8+
9+
10+
```js
11+
charlie();
12+
```
13+
14+
```js
15+
foxtrot();
16+
```
17+
18+
```
19+
golf hotel(); india
20+
```
21+
22+
```
23+
juliet kilo();
24+
```
25+
26+
```
27+
lima(); mike
28+
```
29+
30+
```
31+
```
32+
33+
```
34+
november oscar(); papa
35+
```

tests/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ test('fixtures', function (t) {
5656
'should produce valid MDAST nodes'
5757
);
5858

59-
st.deepEqual(remark.stringify(tree), output || '\n', 'should produce the same documents');
59+
if (!config || config.stringify !== false) {
60+
st.deepEqual(remark.stringify(tree), output || '\n', 'should produce the same documents');
61+
}
6062

6163
st.deepEqual(
6264
tree,

0 commit comments

Comments
 (0)