diff --git a/Scripts/Comment Text Block w Paragraph.js b/Scripts/Comment Text Block w Paragraph.js new file mode 100644 index 00000000..9ac3f409 --- /dev/null +++ b/Scripts/Comment Text Block w Paragraph.js @@ -0,0 +1,80 @@ +/** + { + "api": 1, + "name": "Commented Text Block w/ Paragraph", + "description": "Converts a block of text into a commented text block with max length of 66 characters and line breaks before or after words.", + "author": "Your Name", + "icon": "quote", + "tags": "comment,text,line break" + } +**/ + +function main(state) { + try { + // Extract the text from the state + const text = state.fullText; + + // Split the text into paragraphs + const paragraphs = text.split('\n\n'); + + // Process each paragraph separately + const commentedParagraphs = paragraphs.map(processParagraph); + + // Join the paragraphs with paragraph breaks and assign the commented text back to the state.text property + state.text = commentedParagraphs.join('\n\n'); + } catch (error) { + state.postError("Something went wrong while processing the text: " + error.message); + } +} + +function processParagraph(paragraph) { + // Split the paragraph into words + const words = paragraph.split(/\s+/); + + // Create commented lines with line breaks before or after words + const commentedLines = createCommentedLines(words); + + // Join the lines with line breaks + const commentedParagraph = commentedLines.join('\n'); + + return commentedParagraph; +} + +function createCommentedLines(words) { + const lines = []; + let currentLine = ''; + let lineLength = 0; + + for (const word of words) { + const wordLength = word.length; + + // Check if adding the word would exceed the character limit + if (lineLength + wordLength + 3 > 66) { // 3 accounts for comment markers and space + + // Add the current line to the list + lines.push(currentLine); + + // Reset the line for the next line + currentLine = ''; + lineLength = 0; + } + + // Add the word to the current line + if (currentLine !== '') { + currentLine += ' '; + lineLength++; + } + currentLine += word; + lineLength += wordLength; + } + + // Add the last line to the list + if (currentLine !== '') { + lines.push(currentLine); + } + + // Add comment markers to each line + const commentedLines = lines.map(line => `// ${line}`); + + return commentedLines; +} diff --git a/Scripts/Comment Text Block.js b/Scripts/Comment Text Block.js new file mode 100644 index 00000000..7c1ad057 --- /dev/null +++ b/Scripts/Comment Text Block.js @@ -0,0 +1,70 @@ +/** + { + "api": 1, + "name": "Commented Text Block", + "description": "Converts a block of text into a commented text block with max length of 66 characters and line breaks before or after words.", + "author": "Your Name", + "icon": "quote", + "tags": "comment,text,line break" + } +**/ + +function main(state) { + try { + // Extract the text from the state + const text = state.fullText; + + // Split the text into words + const words = text.split(/\s+/); + + // Create commented lines with line breaks before or after words + const commentedLines = createCommentedLines(words); + + // Join the lines with line breaks + const commentedText = commentedLines.join('\n'); + + // Assign the commented text back to the state.text property + state.text = commentedText; + } catch (error) { + state.postError("Something went wrong while processing the text: " + error.message); + } +} + +function createCommentedLines(words) { + const lines = []; + let currentLine = ''; + let lineLength = 0; + + for (const word of words) { + const wordLength = word.length; + + // Check if adding the word would exceed the character limit + if (lineLength + wordLength + 3 > 66) { // 3 accounts for comment markers and space + + // Add the current line to the list + lines.push(currentLine); + + // Reset the line for the next line + currentLine = ''; + lineLength = 0; + } + + // Add the word to the current line + if (currentLine !== '') { + currentLine += ' '; + lineLength++; + } + currentLine += word; + lineLength += wordLength; + } + + // Add the last line to the list + if (currentLine !== '') { + lines.push(currentLine); + } + + // Add comment markers to each line + const commentedLines = lines.map(line => `// ${line}`); + + return commentedLines; +} diff --git a/Scripts/Uncomment Text Block w Paragraph.js b/Scripts/Uncomment Text Block w Paragraph.js new file mode 100644 index 00000000..c9187f07 --- /dev/null +++ b/Scripts/Uncomment Text Block w Paragraph.js @@ -0,0 +1,35 @@ +/** + { + "api": 1, + "name": "Uncomment Text w/ Paragraphs", + "description": "Removes comment markers and restores the commented text to a block of text while maintaining paragraph breaks.", + "author": "benniefolyfe", + "icon": "quote", + "tags": "uncomment,text,paragraph breaks" + } +**/ + +function main(state) { + try { + // Extract the text from the state + const text = state.fullText; + + // Split the text into paragraphs + const commentedParagraphs = text.split('\n\n'); + + // Process each commented paragraph separately + const restoredParagraphs = commentedParagraphs.map(restoreParagraph); + + // Join the paragraphs with paragraph breaks and assign the restored text back to the state.text property + state.text = restoredParagraphs.join('\n\n'); + } catch (error) { + state.postError("Something went wrong while processing the text: " + error.message); + } +} + +function restoreParagraph(commentedParagraph) { + // Remove comment markers and leading whitespace from each line + const lines = commentedParagraph.split('\n').map(line => line.replace(/\/\/\s?/g, '').trim()); + + return lines.join(' '); +} diff --git a/Scripts/Uncomment Text Block.js b/Scripts/Uncomment Text Block.js new file mode 100644 index 00000000..8ddc081a --- /dev/null +++ b/Scripts/Uncomment Text Block.js @@ -0,0 +1,28 @@ +/** + { + "api": 1, + "name": "Uncomment and Restore", + "description": "Removes comment markers and restores the commented text to a cohesive paragraph.", + "author": "benniefolyfe", + "icon": "quote", + "tags": "uncomment,text" + } +**/ + +function main(state) { + try { + // Extract the text from the state + const text = state.fullText; + + // Remove comment markers and leading whitespace from each line + const uncommentedLines = text.split('\n').map(line => line.replace(/\/\/\s?/g, '').trim()); + + // Join the lines into a cohesive paragraph + const uncommentedText = uncommentedLines.join(' '); + + // Assign the uncommented text back to the state.text property + state.text = uncommentedText; + } catch (error) { + state.postError("Something went wrong while processing the text: " + error.message); + } +}