Skip to content

Commit

Permalink
fix: change language of code block to lowercase (fix #1004) (#1005)
Browse files Browse the repository at this point in the history
* fix: change language of code block to lowercase (fix #1004)

* feat: apply code review
  • Loading branch information
seonim-ryu authored Jun 2, 2020
1 parent 1f6a54d commit 609e89d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
4 changes: 4 additions & 0 deletions apps/editor/src/js/codeBlockManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class CodeBlockManager {
* @param {function} replacer - replacer function to code block element
*/
setReplacer(language, replacer) {
language = language.toLowerCase();

this._replacers[language] = replacer;
}

Expand All @@ -35,6 +37,8 @@ class CodeBlockManager {
* @returns {string}
*/
createCodeBlockHtml(language, codeText) {
language = language.toLowerCase();

const replacer = this.getReplacer(language);

if (replacer) {
Expand Down
40 changes: 28 additions & 12 deletions apps/editor/test/unit/codeBlockManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,55 @@ describe('CodeBlockManager', () => {
});

describe('setReplacer', () => {
it('set replacer for code block element', () => {
const type = 'tui.grid';
it('sets replacer for code block element', () => {
const replacer = {
viewer: true
};

expect(codeBlockManager.getReplacer(type)).toBeUndefined();
expect(codeBlockManager.getReplacer('tui.grid')).toBeUndefined();

codeBlockManager.setReplacer(type, replacer);
codeBlockManager.setReplacer('tui.grid', replacer);

expect(codeBlockManager.getReplacer(type)).toBe(replacer);
expect(codeBlockManager.getReplacer('tui.grid')).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.', () => {
const type = 'awesome-languages';
it('creates a code block html when there is a registered replacer', () => {
const replacer = codeText => `replaced ${codeText} here`;

codeBlockManager.setReplacer(type, replacer);
codeBlockManager.setReplacer('awesome-languages', replacer);

const actual = codeBlockManager.createCodeBlockHtml(type, 'var a = 1;');
const actual = codeBlockManager.createCodeBlockHtml('awesome-languages', 'var a = 1;');
const expected = 'replaced var a = 1; here';

expect(actual).toBe(expected);
});

it('Create 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;');
it('creates code block html if there is no registered replacer and not highlight.js type', () => {
const actual = codeBlockManager.createCodeBlockHtml('tui.grid', '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 replacer = jasmine.createSpy('replacer');

codeBlockManager.setReplacer('PlantUML', replacer);
codeBlockManager.createCodeBlockHtml('PlantUML', 'foo');

expect(replacer.calls.mostRecent().args[1]).toBe('plantuml');
});
});
});

0 comments on commit 609e89d

Please sign in to comment.