| 1 | // Showdown extension that replaces words surrounded by singular underscores with <em> tags |
| 2 | export 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 | |
| 30 | function canUseNegativeLookbehind() { |
| 31 | try { |
| 32 | new RegExp('(?<!_)'); |
| 33 | return true; |
| 34 | } catch (e) { |
| 35 | return false; |
| 36 | } |
| 37 | } |