2017-12-17 06:35:30 +01:00
|
|
|
/**
|
|
|
|
* Replace fontawesome symbols
|
|
|
|
*/
|
|
|
|
|
2018-09-16 20:02:58 +02:00
|
|
|
import * as fontawesome from '@fortawesome/fontawesome-svg-core';
|
|
|
|
import { far } from '@fortawesome/free-regular-svg-icons';
|
|
|
|
import { fas } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import { fab } from '@fortawesome/free-brands-svg-icons';
|
2017-12-17 06:35:30 +01:00
|
|
|
|
2018-09-16 20:02:58 +02:00
|
|
|
fontawesome.library.add(far, fas, fab);
|
2017-12-17 06:35:30 +01:00
|
|
|
|
|
|
|
export const pattern = /%fa:(.+?)%/g;
|
|
|
|
|
2018-06-17 12:09:24 +02:00
|
|
|
export const replacement = (match: string, key: string) => {
|
2017-12-17 06:35:30 +01:00
|
|
|
const args = key.split(' ');
|
|
|
|
let prefix = 'fas';
|
2018-06-17 12:09:24 +02:00
|
|
|
const classes: string[] = [];
|
2017-12-17 06:35:30 +01:00
|
|
|
let transform = '';
|
|
|
|
let name;
|
|
|
|
|
|
|
|
args.forEach(arg => {
|
|
|
|
if (arg == 'R' || arg == 'S' || arg == 'B') {
|
|
|
|
prefix =
|
|
|
|
arg == 'R' ? 'far' :
|
|
|
|
arg == 'S' ? 'fas' :
|
|
|
|
arg == 'B' ? 'fab' :
|
|
|
|
'';
|
2018-08-25 15:42:26 +02:00
|
|
|
} else if (arg.startsWith('.')) {
|
2018-09-01 16:12:51 +02:00
|
|
|
classes.push(`fa-${arg.substr(1)}`);
|
2018-08-25 15:42:26 +02:00
|
|
|
} else if (arg.startsWith('-')) {
|
2017-12-17 06:35:30 +01:00
|
|
|
transform = arg.substr(1).split('|').join(' ');
|
|
|
|
} else {
|
|
|
|
name = arg;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-17 12:09:24 +02:00
|
|
|
const icon = fontawesome.icon({ prefix, iconName: name } as fontawesome.IconLookup, {
|
|
|
|
classes: classes,
|
|
|
|
transform: fontawesome.parse.transform(transform)
|
2017-12-17 06:35:30 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (icon) {
|
|
|
|
return `<i data-fa class="${name}">${icon.html[0]}</i>`;
|
|
|
|
} else {
|
|
|
|
console.warn(`'${name}' not found in fa`);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default (src: string) => {
|
|
|
|
return src.replace(pattern, replacement);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const fa = fontawesome;
|