mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-03 02:11:07 +05:30
feat(monospace): add settings schema with safe defaults
This commit is contained in:
@@ -0,0 +1,49 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const FAMILY_BY_KEY = {
|
||||||
|
'jetbrains-mono': 'JetBrains Mono',
|
||||||
|
'fira-code': 'Fira Code',
|
||||||
|
};
|
||||||
|
|
||||||
|
const ALLOWED_FONTS = Object.keys(FAMILY_BY_KEY);
|
||||||
|
|
||||||
|
const DEFAULT_SETTINGS = Object.freeze({
|
||||||
|
monospaceFont: 'jetbrains-mono',
|
||||||
|
monospaceLigatures: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
function getActiveMonoFont(settings) {
|
||||||
|
const key = settings && settings.monospaceFont;
|
||||||
|
if (typeof key === 'string' && Object.prototype.hasOwnProperty.call(FAMILY_BY_KEY, key)) {
|
||||||
|
return FAMILY_BY_KEY[key];
|
||||||
|
}
|
||||||
|
return FAMILY_BY_KEY[DEFAULT_SETTINGS.monospaceFont];
|
||||||
|
}
|
||||||
|
|
||||||
|
function isLigaturesEnabled(settings) {
|
||||||
|
return !!(settings && settings.monospaceLigatures === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function safeMonospaceSettings(input) {
|
||||||
|
const safe = { ...DEFAULT_SETTINGS };
|
||||||
|
if (input && typeof input === 'object') {
|
||||||
|
if (ALLOWED_FONTS.includes(input.monospaceFont)) {
|
||||||
|
safe.monospaceFont = input.monospaceFont;
|
||||||
|
}
|
||||||
|
if (typeof input.monospaceLigatures === 'boolean') {
|
||||||
|
safe.monospaceLigatures = input.monospaceLigatures;
|
||||||
|
} else if (input.monospaceLigatures === 1 || input.monospaceLigatures === 'true') {
|
||||||
|
safe.monospaceLigatures = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return safe;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
FAMILY_BY_KEY,
|
||||||
|
ALLOWED_FONTS,
|
||||||
|
DEFAULT_SETTINGS,
|
||||||
|
getActiveMonoFont,
|
||||||
|
isLigaturesEnabled,
|
||||||
|
safeMonospaceSettings,
|
||||||
|
};
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
const {
|
||||||
|
FAMILY_BY_KEY,
|
||||||
|
getActiveMonoFont,
|
||||||
|
isLigaturesEnabled,
|
||||||
|
safeMonospaceSettings,
|
||||||
|
DEFAULT_SETTINGS,
|
||||||
|
} = require('../../../../src/main/settings/monospaceSettings');
|
||||||
|
|
||||||
|
describe('monospaceSettings', () => {
|
||||||
|
test('FAMILY_BY_KEY maps jetbrains-mono → "JetBrains Mono"', () => {
|
||||||
|
expect(FAMILY_BY_KEY['jetbrains-mono']).toBe('JetBrains Mono');
|
||||||
|
});
|
||||||
|
test('getActiveMonoFont returns family for valid key', () => {
|
||||||
|
expect(getActiveMonoFont({ monospaceFont: 'fira-code' })).toBe('Fira Code');
|
||||||
|
});
|
||||||
|
test('getActiveMonoFont falls back to default for unknown key', () => {
|
||||||
|
expect(getActiveMonoFont({ monospaceFont: 'unknown' })).toBe('JetBrains Mono');
|
||||||
|
});
|
||||||
|
test('getActiveMonoFont returns default when key missing', () => {
|
||||||
|
expect(getActiveMonoFont({})).toBe('JetBrains Mono');
|
||||||
|
});
|
||||||
|
test('isLigaturesEnabled is true only when explicitly true', () => {
|
||||||
|
expect(isLigaturesEnabled({ monospaceLigatures: true })).toBe(true);
|
||||||
|
expect(isLigaturesEnabled({ monospaceLigatures: false })).toBe(false);
|
||||||
|
expect(isLigaturesEnabled({ monospaceLigatures: 'yes' })).toBe(false);
|
||||||
|
expect(isLigaturesEnabled({})).toBe(false);
|
||||||
|
});
|
||||||
|
test('safeMonospaceSettings rejects unknown fonts', () => {
|
||||||
|
expect(safeMonospaceSettings({ monospaceFont: 'comic-sans' })).toEqual(DEFAULT_SETTINGS);
|
||||||
|
});
|
||||||
|
test('safeMonospaceSettings coerces ligatures to boolean', () => {
|
||||||
|
expect(safeMonospaceSettings({ monospaceLigatures: 1 })).toEqual({
|
||||||
|
monospaceFont: 'jetbrains-mono',
|
||||||
|
monospaceLigatures: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('safeMonospaceSettings returns DEFAULT_SETTINGS for empty input', () => {
|
||||||
|
expect(safeMonospaceSettings({})).toEqual(DEFAULT_SETTINGS);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user