소스 검색

Fenced code blocks support language modifier

main
Rocketsoup 1 년 전
부모
커밋
3b7e941c75
3개의 변경된 파일20개의 추가작업 그리고 5개의 파일을 삭제
  1. 12
    4
      js/markdown.js
  2. 1
    1
      js/markdown.min.js
  3. 7
    0
      testjs.html

+ 12
- 4
js/markdown.js 파일 보기

1447
 		let openFenceLine = state.lines[p++];
1447
 		let openFenceLine = state.lines[p++];
1448
 		var modifier;
1448
 		var modifier;
1449
 		[openFenceLine, modifier] = MDTagModifier.fromLine(openFenceLine);
1449
 		[openFenceLine, modifier] = MDTagModifier.fromLine(openFenceLine);
1450
-		if (openFenceLine.trim() != '```') return null;
1450
+		const match = /^```\s*([a-z0-9]*)\s*$/.exec(openFenceLine);
1451
+		if (match === null) return null;
1452
+		const language = match[1].length > 0 ? match[1] : null;
1451
 		var codeLines = [];
1453
 		var codeLines = [];
1452
 		while (state.hasLines(1, p)) {
1454
 		while (state.hasLines(1, p)) {
1453
 			let line = state.lines[p++];
1455
 			let line = state.lines[p++];
1454
 			if (line.trim() == '```') {
1456
 			if (line.trim() == '```') {
1455
 				state.p = p;
1457
 				state.p = p;
1456
-				let block = new MDCodeBlockNode(codeLines.join("\n"));
1458
+				let block = new MDCodeBlockNode(codeLines.join("\n"), language);
1457
 				if (modifier) modifier.applyTo(block);
1459
 				if (modifier) modifier.applyTo(block);
1458
 				return block;
1460
 				return block;
1459
 			}
1461
 			}
2594
 	/** @type {string} */
2596
 	/** @type {string} */
2595
 	text;
2597
 	text;
2596
 
2598
 
2599
+	/** @type {string|null} */
2600
+	language;
2601
+
2597
 	/**
2602
 	/**
2598
 	 * @param {string} text
2603
 	 * @param {string} text
2604
+	 * @param {string|null} language
2599
 	 */
2605
 	 */
2600
-	constructor(text) {
2606
+	constructor(text, language=null) {
2601
 		super([]);
2607
 		super([]);
2602
 		this.text = text;
2608
 		this.text = text;
2609
+		this.language = language;
2603
 	}
2610
 	}
2604
 
2611
 
2605
 	toHTML(state) {
2612
 	toHTML(state) {
2606
-		return `<pre${this._htmlAttributes()}><code>${MDUtils.escapeHTML(this.text)}</code></pre>\n`;
2613
+		const languageModifier = (this.language !== null) ? ` class="language-${this.language}"` : '';
2614
+		return `<pre${this._htmlAttributes()}><code${languageModifier}>${MDUtils.escapeHTML(this.text)}</code></pre>\n`;
2607
 	}
2615
 	}
2608
 }
2616
 }
2609
 
2617
 

+ 1
- 1
js/markdown.min.js
파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
파일 보기


+ 7
- 0
testjs.html 파일 보기

1018
 					this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
1018
 					this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
1019
 				}
1019
 				}
1020
 
1020
 
1021
+				test_codeBlock_fenced_language() {
1022
+					let markdown = "Code\n\n```javascript\nfunction foo() {\n    return 'bar';\n}\n```\n\nend";
1023
+					let expected = "<p>Code</p>\n\n<pre><code class=\"language-javascript\">function foo() {\n    return 'bar';\n}</code></pre>\n\n<p>end</p>";
1024
+					let actual = this.parser.toHTML(markdown).trim(); // don't normalize whitespace
1025
+					this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
1026
+				}
1027
+
1021
 				test_horizontalRule() {
1028
 				test_horizontalRule() {
1022
 					let markdown = "Before\n\n---\n\n- - -\n\n***\n\n* * * * * * *\n\nafter";
1029
 					let markdown = "Before\n\n---\n\n- - -\n\n***\n\n* * * * * * *\n\nafter";
1023
 					let expected = "<p>Before</p> <hr> <hr> <hr> <hr> <p>after</p>";
1030
 					let expected = "<p>Before</p> <hr> <hr> <hr> <hr> <p>after</p>";

Loading…
취소
저장