From 49d5df3e8bd6beee8738a7be1890f0dda640c6a1 Mon Sep 17 00:00:00 2001 From: seonim-ryu Date: Mon, 25 May 2020 21:13:17 +0900 Subject: [PATCH] fix: change language of code block to lowercase (fix #1004) --- apps/editor/src/js/codeBlockManager.js | 8 ++++++ .../editor/test/unit/codeBlockManager.spec.js | 26 ++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/apps/editor/src/js/codeBlockManager.js b/apps/editor/src/js/codeBlockManager.js index 8417c5e356..10a210ff9d 100644 --- a/apps/editor/src/js/codeBlockManager.js +++ b/apps/editor/src/js/codeBlockManager.js @@ -10,12 +10,18 @@ class CodeBlockManager { this._replacers = {}; } + _changeLanguageToLowerCase(language) { + return language.toLowerCase(); + } + /** * Set replacer for code block * @param {string} language - code block language * @param {function} replacer - replacer function to code block element */ setReplacer(language, replacer) { + language = this._changeLanguageToLowerCase(language); + this._replacers[language] = replacer; } @@ -35,6 +41,8 @@ class CodeBlockManager { * @returns {string} */ createCodeBlockHtml(language, codeText) { + language = this._changeLanguageToLowerCase(language); + const replacer = this.getReplacer(language); if (replacer) { diff --git a/apps/editor/test/unit/codeBlockManager.spec.js b/apps/editor/test/unit/codeBlockManager.spec.js index 8f4aafe106..b6ab75ec22 100644 --- a/apps/editor/test/unit/codeBlockManager.spec.js +++ b/apps/editor/test/unit/codeBlockManager.spec.js @@ -12,7 +12,7 @@ describe('CodeBlockManager', () => { }); describe('setReplacer', () => { - it('set replacer for code block element', () => { + it('sets replacer for code block element', () => { const type = 'tui.grid'; const replacer = { viewer: true @@ -24,10 +24,20 @@ describe('CodeBlockManager', () => { expect(codeBlockManager.getReplacer(type)).toBe(replacer); }); + + it('saves the replacer name in lowercase', () => { + const replacer = {}; + + codeBlockManager.setReplacer('UML', replacer); + expect(codeBlockManager.getReplacer('uml')).toBe(replacer); + + codeBlockManager.setReplacer('Uml', replacer); + expect(codeBlockManager.getReplacer('uml')).toBe(replacer); + }); }); describe('createCodeBlockHtml', () => { - it('Create a code block html when there is a registered replacer.', () => { + it('creates a code block html when there is a registered replacer', () => { const type = 'awesome-languages'; const replacer = codeText => `replaced ${codeText} here`; @@ -39,12 +49,22 @@ describe('CodeBlockManager', () => { expect(actual).toBe(expected); }); - it('Create code block html if there is no registered replacer and not highlight.js type.', () => { + it('creates code block html if there is no registered replacer and not highlight.js type', () => { const type = 'tui.grid'; const actual = codeBlockManager.createCodeBlockHtml(type, 'var a = 1;'); const expected = 'var a = 1;'; expect(actual).toBe(expected); }); + + it('converts the language name to lowercase and call the replacer with it', () => { + const language = 'PlantUML'; + const replacer = jasmine.createSpy('replacer'); + + codeBlockManager.setReplacer(language, replacer); + codeBlockManager.createCodeBlockHtml(language, 'foo'); + + expect(replacer.calls.mostRecent().args[1]).toBe('plantuml'); + }); }); });