diff options
Diffstat (limited to 'app/assets/javascripts/behaviors')
-rw-r--r-- | app/assets/javascripts/behaviors/markdown/utils.js | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/app/assets/javascripts/behaviors/markdown/utils.js b/app/assets/javascripts/behaviors/markdown/utils.js new file mode 100644 index 00000000000..f02d6c0f813 --- /dev/null +++ b/app/assets/javascripts/behaviors/markdown/utils.js @@ -0,0 +1,27 @@ +/** + * This method parses raw markdown text in GFM input field and toggles checkboxes + * based on checkboxChecked property. + * + * @param {Object} object containing rawMarkdown, sourcepos, checkboxChecked properties + * @returns String with toggled checkboxes + */ +export const toggleMarkCheckboxes = ({ rawMarkdown, sourcepos, checkboxChecked }) => { + // Extract the description text + const [startRange] = sourcepos.split('-'); + let [startRow] = startRange.split(':'); + startRow = Number(startRow) - 1; + + // Mark/Unmark the checkboxes + return rawMarkdown + .split('\n') + .map((row, index) => { + if (startRow === index) { + if (checkboxChecked) { + return row.replace(/\[ \]/, '[x]'); + } + return row.replace(/\[[x~]\]/i, '[ ]'); + } + return row; + }) + .join('\n'); +}; |