Skip to content

Commit d099c68

Browse files
mxstbrsstur
authored andcommitted
Add failing test for getting the language from fenced code blocks (sstur#167)
* Add failing test for getting language from fenced code blocks * Correctly extract language from markdown code blocks * Workaround Flow error in node_modules
1 parent a68f021 commit d099c68

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

.flowconfig

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[ignore]
2+
.*/node_modules/@lerna/.*
23

34
[include]
45

packages/draft-js-import-element/src/stateFromElement.js

+6
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ class ContentGenerator {
294294
isCustomType = false;
295295
type = this.getBlockTypeFromTagName(tagName);
296296
}
297+
if (type === BLOCK_TYPE.CODE) {
298+
let language = element.getAttribute('data-language');
299+
if (language) {
300+
data = {...data, language};
301+
}
302+
}
297303
let hasDepth = canHaveDepth(type);
298304
let allowRender = !SPECIAL_ELEMENTS.hasOwnProperty(tagName);
299305
if (!isCustomType && !hasSemanticMeaning(type)) {

packages/draft-js-import-markdown/src/MarkdownParser.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,14 @@ function Renderer(options) {
594594
}
595595

596596
Renderer.prototype.code = function(text, lang) {
597-
var attributes = [];
597+
var codeAttrs = [];
598+
var preAttrs = [];
598599
if (lang) {
599-
attributes.push({name: 'class', value: this.options.langPrefix + lang});
600+
codeAttrs.push({name: 'class', value: this.options.langPrefix + lang});
601+
preAttrs.push({name: 'data-language', value: lang});
600602
}
601-
var codeNode = new ElementNode('code', attributes, [new TextNode(text)]);
602-
return new ElementNode('pre', [], [codeNode]);
603+
var codeNode = new ElementNode('code', codeAttrs, [new TextNode(text)]);
604+
return new ElementNode('pre', preAttrs, [codeNode]);
603605
};
604606

605607
Renderer.prototype.blockquote = function(childNode) {

packages/draft-js-import-markdown/src/__tests__/stateFromMarkdown-test.js

+24
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ describe('stateFromMarkdown', () => {
4343
},
4444
]);
4545
});
46+
it('should correctly handle code blocks with languages', () => {
47+
let markdown = "```javascript\nconst a = 'b'\n```";
48+
let contentState = stateFromMarkdown(markdown);
49+
let rawContentState = convertToRaw(contentState);
50+
let blocks = removeKeys(rawContentState.blocks);
51+
expect(blocks).toEqual([
52+
{
53+
text: "const a = 'b'",
54+
type: 'code-block',
55+
data: {
56+
language: 'javascript',
57+
},
58+
depth: 0,
59+
inlineStyleRanges: [
60+
{
61+
length: 13,
62+
offset: 0,
63+
style: 'CODE',
64+
},
65+
],
66+
entityRanges: [],
67+
},
68+
]);
69+
});
4670
it('should correctly handle linebreaks option', () => {
4771
let markdown = 'Hello\nWorld';
4872
let contentState = stateFromMarkdown(markdown, {

0 commit comments

Comments
 (0)