Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Templater Template for Obsidian - Text Underlining Toggle
- *
- * This template provides a toggle functionality for underlining selected text in Obsidian notes.
- * It handles the following operations:
- * 1. If text is not underlined: Adds HTML underlining tags and converts newlines to <br> tags
- * 2. If text is already underlined: Removes underlining and converts <br> tags back to newlines
- *
- * Usage:
- * 1. Select text in your Obsidian note
- * 2. Trigger this template
- * 3. The text will toggle between underlined and non-underlined states
- */
- <%*
- // Get the currently selected text from the editor
- const text = tp.file.selection();
- /**
- * Checks if the text already has HTML underlining tags
- * @param {string} text - The text to check
- * @returns {boolean} - True if text has underlining tags, false otherwise
- */
- const hasUnderlineTags = text => {
- return text.startsWith('<u>') && text.endsWith('</u>');
- };
- /**
- * Converts newlines to HTML break tags
- * @param {string} text - The text to process
- * @returns {string} - Text with newlines replaced by <br> tags
- */
- const addBreakTags = text => {
- return text.replace(/\n/g, '<br>');
- };
- /**
- * Converts HTML break tags back to newlines
- * @param {string} text - The text to process
- * @returns {string} - Text with <br> tags replaced by newlines
- */
- const removeBreakTags = text => {
- return text.replace(/<br>/g, '\n');
- };
- let result;
- if (hasUnderlineTags(text)) {
- // If text is already underlined, remove the formatting
- result = text
- .replace('<u>', '') // Remove opening underline tag
- .replace('</u>', '') // Remove closing underline tag
- .replace(/<br>/g, '\n'); // Convert break tags back to newlines
- } else {
- // If text is not underlined, add the formatting
- result = `<u>${addBreakTags(text)}</u>`;
- }
- // Return the processed text to replace the selection
- tR += result;
- %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement