Skip to content

Commit

Permalink
feat: ✨ add ability to change header and footer prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjackphil committed May 1, 2022
1 parent 795c312 commit d93e20d
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ interface PluginSettings {
defaultTemplate: string
excludeCurrent: boolean
autoExpand: boolean
prefixes: {
header: string
footer: string
}
}

type NumberTuple = [number, number]
Expand Down Expand Up @@ -58,7 +62,11 @@ export default class TextExpander extends Plugin {
defaultTemplate: '- $link',
delay: 300,
excludeCurrent: true,
lineEnding: '<-->'
lineEnding: '<-->',
prefixes: {
header: '^',
footer: '>'
}
}

seqs: Sequences[] = sequences
Expand Down Expand Up @@ -142,18 +150,22 @@ export default class TextExpander extends Plugin {
})
}

async startTemplateMode(query: ExpanderQuery, lastLine: number) {
async startTemplateMode(query: ExpanderQuery, lastLine: number, prefixes: PluginSettings["prefixes"]) {
const currentView = this.app.workspace.activeLeaf.view
let currentFileName = ''

const templateContent = query.template.split('\n')

const heading = templateContent.filter(e => e[0] === '^').map((s) => s.slice(1))
const footer = templateContent.filter(e => e[0] === '>').map((s) => s.slice(1))
const isHeader = (line: string) => line.startsWith(prefixes.header)
const isFooter = (line: string) => line.startsWith(prefixes.footer)
const isRepeat = (line: string) => !isHeader(line) && !isFooter(line)

const heading = templateContent.filter(isHeader).map((s) => s.slice(1))
const footer = templateContent.filter(isFooter).map((s) => s.slice(1))
const repeatableContent =
templateContent.filter(e => e[0] !== '^' && e[0] !== '>').filter(e => e).length === 0
templateContent.filter(isRepeat).filter(e => e).length === 0
? [this.config.defaultTemplate]
: templateContent.filter(e => e[0] !== '^' && e[0] !== '>').filter(e => e)
: templateContent.filter(isRepeat).filter(e => e)

if (currentView instanceof FileView) {
currentFileName = currentView.file.basename
Expand Down Expand Up @@ -209,7 +221,7 @@ export default class TextExpander extends Plugin {
}

async runQuery(query: ExpanderQuery, content: string[]) {
const { lineEnding } = this.config
const { lineEnding, prefixes } = this.config

if (!query) {
new Notification('Expand query not found')
Expand All @@ -224,7 +236,7 @@ export default class TextExpander extends Plugin {
const newContent = formatContent(this.cm.getValue())

this.search(query.query)
return await this.startTemplateMode(query, getLastLineToReplace(newContent, query, this.config.lineEnding))
return await this.startTemplateMode(query, getLastLineToReplace(newContent, query, this.config.lineEnding), prefixes)
}

initExpander(all = false) {
Expand Down Expand Up @@ -387,6 +399,32 @@ class SettingTab extends PluginSettingTab {
})
})

new Setting(containerEl)
.setHeading()
.setName('Prefixes')

new Setting(containerEl)
.setName('Header')
.setDesc('Line prefixed by this symbol will be recognized as header')
.addText(text => {
text.setValue(this.plugin.config.prefixes.header)
.onChange(val => {
this.plugin.config.prefixes.header = val
this.plugin.saveSettings()
})
})

new Setting(containerEl)
.setName('Footer')
.setDesc('Line prefixed by this symbol will be recognized as footer')
.addText(text => {
text.setValue(this.plugin.config.prefixes.footer)
.onChange(val => {
this.plugin.config.prefixes.footer = val
this.plugin.saveSettings()
})
})

new Setting(containerEl)
.setName('Sequences')
.setDesc('REGEXP - DESCRIPTION')
Expand Down

0 comments on commit d93e20d

Please sign in to comment.