2024-03-03 14:26:25 +01:00
|
|
|
// Showdown extension that replaces words surrounded by singular underscores with <em> tags
|
|
|
|
export const markdownUnderscoreExt = () => {
|
2024-03-03 18:36:23 +01:00
|
|
|
try {
|
|
|
|
if (!canUseNegativeLookbehind()) {
|
|
|
|
console.log('Showdown-underscore extension: Negative lookbehind not supported. Skipping.');
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [{
|
2024-08-19 00:26:27 +02:00
|
|
|
type: 'output',
|
2024-08-25 20:17:59 +02:00
|
|
|
regex: new RegExp('(<code(?:\\s+[^>]*)?>[\\s\\S]*?<\\/code>)|\\b(?<!_)_(?!_)(.*?)(?<!_)_(?!_)\\b', 'g'),
|
2024-08-19 00:26:27 +02:00
|
|
|
replace: function(match, codeContent, italicContent) {
|
|
|
|
if (codeContent) {
|
|
|
|
// If it's inside <code> tags, return unchanged
|
2024-08-18 12:58:47 +02:00
|
|
|
return match;
|
2024-08-18 13:15:22 +02:00
|
|
|
} else if (italicContent) {
|
2024-08-18 12:58:47 +02:00
|
|
|
// If it's an italic group, apply the replacement
|
2024-08-18 13:15:22 +02:00
|
|
|
return '<em>' + italicContent + '</em>';
|
2024-08-18 12:58:47 +02:00
|
|
|
}
|
2024-08-18 13:21:50 +02:00
|
|
|
// If none of the conditions are met, return the original match
|
2024-08-18 13:15:22 +02:00
|
|
|
return match;
|
2024-08-18 12:58:47 +02:00
|
|
|
},
|
2024-03-03 18:36:23 +01:00
|
|
|
}];
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Error in Showdown-underscore extension:', e);
|
2024-03-03 14:26:25 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function canUseNegativeLookbehind() {
|
|
|
|
try {
|
|
|
|
new RegExp('(?<!_)');
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|