Blame Raw
Cohee · e3f41666 · · 37 lines (1.3 KB)
1 contributor
1// Showdown extension that replaces words surrounded by singular underscores with <em> tags
2export const markdownUnderscoreExt = () => {
3 try {
4 if (!canUseNegativeLookbehind()) {
5 console.log('Showdown-underscore extension: Negative lookbehind not supported. Skipping.');
6 return [];
7 }
8
9 return [{
10 type: 'output',
11 regex: new RegExp('(<code(?:\\s+[^>]*)?>[\\s\\S]*?<\\/code>|<style(?:\\s+[^>]*)?>[\\s\\S]*?<\\/style>)|\\b(?<!_)_(?!_)(.*?)(?<!_)_(?!_)\\b', 'gi'),
12 replace: function (match, tagContent, italicContent) {
13 if (tagContent) {
14 // If it's inside <code> or <style> tags, return unchanged
15 return match;
16 } else if (italicContent) {
17 // If it's an italic group, apply the replacement
18 return '<em>' + italicContent + '</em>';
19 }
20 // If none of the conditions are met, return the original match
21 return match;
22 },
23 }];
24 } catch (e) {
25 console.error('Error in Showdown-underscore extension:', e);
26 return [];
27 }
28};
29
30function canUseNegativeLookbehind() {
31 try {
32 new RegExp('(?<!_)');
33 return true;
34 } catch (e) {
35 return false;
36 }
37}