Skip to content

Commit

Permalink
Prepare task parsing for tokenPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
piascikj committed Oct 25, 2024
1 parent 0b90c61 commit 868b463
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
33 changes: 19 additions & 14 deletions lib/adapters/parsers/task/CardContentParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,29 @@ const TASK_TYPES = {
MARKDOWN: 'MARKDOWN',
}


const DEFAULT_TOKEN_PREFIX = '#'
const START_TAG = '<card>'
const END_TAG = '</card>'
const CODE_STYLE_PATTERN = '([A-Z]+[A-Z-_]+?)((:)(-?[\\d.]+(?:e-?\\d+)?)?)?[ \\t]+(.+)$'
const LIST_NAME_PATTERN = '[\\p{L}0-9-_]{2,}' // [a-zA-Z-_]+?
const HASH_STYLE_REGEX = new XRegExp(
`#(${LIST_NAME_PATTERN})(:(-?[\\d.]+(?:e-?\\d+)?)?)?[ \\t]+(.+)$`,
'gm'
)
const HASH_STYLE_META_ORDER_REGEX = new XRegExp(
`#(${LIST_NAME_PATTERN})[ \\t]+(.+)$`, 'gm'
)
const LINK_STYLE_REGEX = new XRegExp(
`(\\[.*\\]\\s?)?(\\[(.+)\\]\\(#(${LIST_NAME_PATTERN})(:)(-?[\\d.]+(?:e-?\\d+)?)?\\))`,
'gm'
)

const CHECK_REGEX = /^(\s*- )\[([x ])\]/

function getHashStyleRegex(tokenPrefix = DEFAULT_TOKEN_PREFIX, metaOrder = false) {
return metaOrder ?
new XRegExp(
`${tokenPrefix}(${LIST_NAME_PATTERN})[ \\t]+(.+)$`, 'gm'
) :
new XRegExp(
`${tokenPrefix}(${LIST_NAME_PATTERN})(:(-?[\\d.]+(?:e-?\\d+)?)?)?[ \\t]+(.+)$`,
'gm'
)
}

function getCheckedData(content) {
const taskRegex = new RegExp(CHECK_REGEX)
const result = taskRegex.exec(content)
Expand Down Expand Up @@ -71,10 +76,10 @@ function isCheckBoxTask (config, line, beforeText) {
function hasTaskInText(config, text, isCodeFile) {
return (
(isCodeFile && hasCodeStyleTask(config, text)) ||
((new XRegExp(HASH_STYLE_REGEX).test(text) ||
new XRegExp(HASH_STYLE_META_ORDER_REGEX).test(text) ||
((new XRegExp(getHashStyleRegex(config.tokenPrefix)).test(text) ||
new XRegExp(getHashStyleRegex(config.tokenPrefix, true)).test(text) ||
new XRegExp(LINK_STYLE_REGEX).test(text)) &&
config.lists.find((list) => text.includes(`#${list.name}`)))
config.lists.find((list) => text.includes(`${config.tokenPrefix}${list.name}`)))
)
}

Expand Down Expand Up @@ -282,7 +287,7 @@ function getTaskContent ({
getTextFileTaskContent({ config, content, beforeText, fileType })
}

function getRawTask ({tokenPrefix = '#', orderMeta, beforeText = '', hasColon = false, list, order= '', text, type}) {
function getRawTask ({tokenPrefix = DEFAULT_TOKEN_PREFIX, orderMeta, beforeText = '', hasColon = false, list, order= '', text, type}) {
if (type === TASK_TYPES.MARKDOWN) {
return `${beforeText}[${text}](${tokenPrefix}${list}:${
orderMeta ? '' : order
Expand All @@ -306,10 +311,10 @@ module.exports = {
isBeforeTextMarkdownList,
padDescription,
getCheckedData,
hasTaskInText,
isNumber,
getHashStyleRegex,
CHECK_REGEX,
LIST_NAME_PATTERN,
HASH_STYLE_REGEX,
HASH_STYLE_META_ORDER_REGEX,
LINK_STYLE_REGEX
}
43 changes: 19 additions & 24 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ var _clone = require('lodash.clone'),
FILE_TYPES,
getTaskContent,
getRawTask,
getHashStyleRegex,
hasTaskInText,
LIST_NAME_PATTERN,
HASH_STYLE_REGEX,
HASH_STYLE_META_ORDER_REGEX,
LINK_STYLE_REGEX,
} = require('./adapters/parsers/task/CardContentParser')

Expand Down Expand Up @@ -346,7 +346,7 @@ File.prototype.extractCodeStyleTasks = function (config, pos, content) {
}

File.prototype.extractHashStyleTasks = function (config, pos, content) {
var hashStyleRegex = new XRegExp(HASH_STYLE_REGEX)
var hashStyleRegex = getHashStyleRegex(config.tokenPrefix)
var lang = this.getLang()
var result
while ((result = hashStyleRegex.exec(content)) !== null) {
Expand Down Expand Up @@ -617,7 +617,7 @@ File.prototype.deleteTask = function (task, config) {
config
)
} else if (task.type === Task.Types.HASHTAG) {
this.deleteCodeOrHashTask(HASH_STYLE_REGEX, task, config)
this.deleteCodeOrHashTask(getHashStyleRegex(config.tokenPrefix), task, config)
}
} else {
const fileContent = this.getContentLines()
Expand Down Expand Up @@ -1095,19 +1095,20 @@ File.prototype.removeTask = function (task) {

File.prototype.ignoreCodeBlocks = function () {
// BACKLOG:-100 This is not used, but we need a way to ignore tasks in code blocks. Maybe save position ranges
var cleanContent = this.content
var replacer = function (block) {
block = block.replace(new XRegExp(LINK_STYLE_REGEX), '**TASK**')
block = block.replace(new XRegExp(HASH_STYLE_REGEX), '**TASK**')
return block
}

if (this.isMarkDownFile()) {
cleanContent = this.content
.replace(new RegExp(CODE_BLOCK_REGEX), replacer)
.replace(new RegExp(INLINE_CODE_REGEX), replacer)
}
return cleanContent
// ```javascript
// var cleanContent = this.content
// var replacer = function (block) {
// block = block.replace(new XRegExp(LINK_STYLE_REGEX), '**TASK**')
// block = block.replace(getHashStyleRegex(config.tokenPrefix), '**TASK**')
// return block
// }
// if (this.isMarkDownFile()) {
// cleanContent = this.content
// .replace(new RegExp(CODE_BLOCK_REGEX), replacer)
// .replace(new RegExp(INLINE_CODE_REGEX), replacer)
// }
// return cleanContent
// ```
}

File.prototype.isMarkDownFile = function () {
Expand Down Expand Up @@ -1245,13 +1246,7 @@ File.prototype.hasCodeStyleTask = function (config, text) {
}

File.prototype.hasTaskInText = function (config, text) {
return (
this.hasCodeStyleTask(config, text) ||
((new XRegExp(HASH_STYLE_REGEX).test(text) ||
new XRegExp(HASH_STYLE_META_ORDER_REGEX).test(text) ||
new XRegExp(LINK_STYLE_REGEX).test(text)) &&
config.lists.find((list) => text.includes(`#${list.name}`)))
)
return hasTaskInText(config, text, this.isCodeFile())
}

File.prototype.isInBlockComment = function (content) {
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/archive-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = class ArchivePlugin extends Plugin {
],
task.content
)
const taskContent = `${task.beforeText}#${task.list} ${content}\n\n\n`
const taskContent = `${task.beforeText}${config.tokenPrefix}${task.list} ${content}\n\n\n`

this.fileGateway.preparePathForWriting(newPath)
this.fileGateway.appendFileSync(newPath, taskContent)
Expand Down

0 comments on commit 868b463

Please sign in to comment.