Skip to content

Commit 8c87c16

Browse files
authored
feat(metadata): html lang attribute (#659)
1 parent 7a9c8ec commit 8c87c16

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

src/html2md.js

+5
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ function addMetadata(hast, mdast) {
172172
}
173173
}
174174

175+
const html = select('html', hast);
176+
if (html.properties?.lang) {
177+
meta.set(text('html-lang'), text(html.properties.lang));
178+
}
179+
175180
if (meta.size) {
176181
mdast.children.push(toGridTable('Metadata', Array.from(meta.entries())));
177182
}

test/fixtures/meta-tags-htmllang.html

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<html lang="en-US">
2+
3+
<head>
4+
<meta property="og:type" content="product">
5+
<meta property="og:image" content="/meta-image.png">
6+
<meta property="product:availability" content="In stock">
7+
<meta property="product:price.amount" content="3">
8+
<meta property="product:price.currency" content="USD">
9+
<meta property="invalid" content="foo">
10+
<meta property="og:ignored" content="bar">
11+
<meta name="twitter:card" content="summary_large_image">
12+
<meta name="twitter:label1" content="Price">
13+
<meta name="twitter:data1" content="$3">
14+
<meta name="twitter:label2" content="Availability">
15+
<meta name="twitter:data2" content="In stock">
16+
<meta name="twitter:image" content="/meta-image.png">
17+
</head>
18+
19+
<body>
20+
<main>
21+
<div>
22+
<h1>Hello, World.</h1>
23+
</div>
24+
</main>
25+
</body>
26+
27+
</html>

test/fixtures/meta-tags-htmllang.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Hello, World.
2+
3+
+------------------------------------------------+
4+
| Metadata |
5+
+------------------------+-----------------------+
6+
| og:type | product |
7+
+------------------------+-----------------------+
8+
| og:image | ![][image0] |
9+
+------------------------+-----------------------+
10+
| product:availability | In stock |
11+
+------------------------+-----------------------+
12+
| product:price.amount | 3 |
13+
+------------------------+-----------------------+
14+
| product:price.currency | USD |
15+
+------------------------+-----------------------+
16+
| twitter:card | summary\_large\_image |
17+
+------------------------+-----------------------+
18+
| twitter:label1 | Price |
19+
+------------------------+-----------------------+
20+
| twitter:data1 | $3 |
21+
+------------------------+-----------------------+
22+
| twitter:label2 | Availability |
23+
+------------------------+-----------------------+
24+
| twitter:data2 | In stock |
25+
+------------------------+-----------------------+
26+
| twitter:image | ![][image0] |
27+
+------------------------+-----------------------+
28+
| html-lang | en-US |
29+
+------------------------+-----------------------+
30+
31+
[image0]: /meta-image.png

test/html2md.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ describe('html2md Tests', () => {
112112
it('convert a document with meta names and properties correctly', async () => {
113113
await test('meta-tags');
114114
});
115+
116+
it('convert a document with html lang correctly', async () => {
117+
await test('meta-tags-htmllang');
118+
});
115119
});
116120

117121
describe('className to block type tests', () => {

0 commit comments

Comments
 (0)