SillyTavern/public/scripts/showdown-underscore.js

38 lines
1.3 KiB
JavaScript
Raw Normal View History

// 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',
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
return match;
2024-08-18 13:15:22 +02:00
} else if (italicContent) {
// If it's an italic group, apply the replacement
2024-08-18 13:15:22 +02:00
return '<em>' + italicContent + '</em>';
}
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-03-03 18:36:23 +01:00
}];
} catch (e) {
console.error('Error in Showdown-underscore extension:', e);
return [];
}
};
function canUseNegativeLookbehind() {
try {
new RegExp('(?<!_)');
return true;
} catch (e) {
return false;
}
}