diff --git a/src/@types/index.d.ts b/src/@types/index.d.ts index 8b8a315..6687f11 100644 --- a/src/@types/index.d.ts +++ b/src/@types/index.d.ts @@ -1,5 +1,3 @@ -import { PaneType } from 'obsidian'; - export interface EmbeddedSlideParameters { slide: string; page?: number; @@ -33,6 +31,15 @@ export interface SlidesExtendedSettings { center: boolean; } +export type ChartJsOptions = { + // biome-ignore lint/suspicious/noExplicitAny: + elements?: any; + // biome-ignore lint/suspicious/noExplicitAny: + plugins?: any; + // biome-ignore lint/suspicious/noExplicitAny: + scales?: any; +}; + export type Options = { bg: string; center: boolean; @@ -55,6 +62,8 @@ export type Options = { width: number; enableCustomControls: boolean; transition: string; + // biome-ignore lint/suspicious/noExplicitAny: + [key: string]: any; }; export interface MediaCollector { diff --git a/src/obsidian/processors/autoClosingProcessor.ts b/src/obsidian/processors/autoClosingProcessor.ts index 764b591..3966cd1 100644 --- a/src/obsidian/processors/autoClosingProcessor.ts +++ b/src/obsidian/processors/autoClosingProcessor.ts @@ -6,7 +6,7 @@ export class AutoClosingProcessor { } transformBlock(markdown: string) { - markdown = markdown.replaceAll(this.regex, `<$1>`); + markdown = markdown.replaceAll(this.regex, "<$1>"); return markdown; } } diff --git a/src/obsidian/processors/blockProcessor.ts b/src/obsidian/processors/blockProcessor.ts index 5ff139e..89e4831 100644 --- a/src/obsidian/processors/blockProcessor.ts +++ b/src/obsidian/processors/blockProcessor.ts @@ -8,7 +8,7 @@ export class BlockProcessor { /:::\sblock\s*/g, '
\n\n', ); - markdown = markdown.replaceAll(':::', '
\n\n'); + markdown = markdown.replaceAll(":::", "\n\n"); return markdown; } } diff --git a/src/obsidian/processors/calloutProcessor.ts b/src/obsidian/processors/calloutProcessor.ts index d84887d..00617fe 100644 --- a/src/obsidian/processors/calloutProcessor.ts +++ b/src/obsidian/processors/calloutProcessor.ts @@ -2,7 +2,7 @@ export class CalloutProcessor { private regex = />\s?\[!([^\]]+)\]-* *(.*)/; process(markdown: string) { - const lineArray = markdown.split('\n'); + const lineArray = markdown.split("\n"); const outArray = []; let startIdx = -1; @@ -10,10 +10,10 @@ export class CalloutProcessor { for (let i = 0; i < lineArray.length; i++) { const line = lineArray[i]; - if (line.trim().startsWith('>') && startIdx == -1) { + if (line.trim().startsWith(">") && startIdx === -1) { startIdx = i; add = false; - } else if (!line.trim().startsWith('>') && startIdx > -1) { + } else if (!line.trim().startsWith(">") && startIdx > -1) { const content = this.transformBlock(lineArray, startIdx, i - 1); for (let index = 0; index < content.length; index++) { @@ -28,7 +28,7 @@ export class CalloutProcessor { outArray.push(lineArray[i]); } } - return outArray.join('\n'); + return outArray.join("\n"); } transformBlock(lines: string[], start: number, end: number) { @@ -44,28 +44,28 @@ export class CalloutProcessor { result.push(`
`); result.push('
'); result.push('
'); - result.push(''); + result.push(""); result.push(icon); - result.push(''); - result.push('
'); + result.push(""); + result.push("
"); result.push('
'); - result.push(''); + result.push(""); result.push(title); - result.push(''); - result.push('
'); - result.push('
'); + result.push(""); + result.push(""); + result.push(""); - if (start != end) { + if (start !== end) { result.push('
'); for (let i = start + 1; i <= end; i++) { - result.push(''); + result.push(""); const line = lines[i].trim().substring(1).trim(); result.push(line); } - result.push(''); - result.push('
'); + result.push(""); + result.push(""); } - result.push(''); + result.push(""); } else { for (let i = start; i <= end; i++) { const line = lines[i]; @@ -79,97 +79,96 @@ export class CalloutProcessor { const input = type.toLowerCase(); switch (input) { - case 'abstract': - case 'summary': - case 'tldr': - case 'info': - return 'callout-color1'; - case 'todo': - case 'tip': - case 'hint': - case 'important': - return 'callout-color2'; - case 'success': - case 'check': - case 'done': - return 'callout-color3'; - case 'question': - case 'help': - case 'faq': - return 'callout-color4'; - case 'warning': - case 'caution': - case 'attention': - return 'callout-color5'; - case 'failure': - case 'fail': - case 'missing': - return 'callout-color6'; - case 'danger': - case 'error': - case 'bug': - return 'callout-color7'; - case 'example': - return 'callout-color8'; - case 'quote': - case 'cite': - return 'callout-color9'; + case "abstract": + case "summary": + case "tldr": + case "info": + return "callout-color1"; + case "todo": + case "tip": + case "hint": + case "important": + return "callout-color2"; + case "success": + case "check": + case "done": + return "callout-color3"; + case "question": + case "help": + case "faq": + return "callout-color4"; + case "warning": + case "caution": + case "attention": + return "callout-color5"; + case "failure": + case "fail": + case "missing": + return "callout-color6"; + case "danger": + case "error": + case "bug": + return "callout-color7"; + case "example": + return "callout-color8"; + case "quote": + case "cite": + return "callout-color9"; default: - return 'callout-color-default'; + return "callout-color-default"; } } titleFrom(icon: string, titleLine: string): string { if (titleLine) { return titleLine; - } else { - return icon[0].toUpperCase() + icon.substring(1).toLowerCase(); } + return icon[0].toUpperCase() + icon.substring(1).toLowerCase(); } iconFrom(type: string): string { const input = type.toLowerCase(); switch (input) { - case 'abstract': - case 'summary': - case 'tldr': - return ':fas_clipboard-list:'; - case 'info': - return ':fas_info-circle:'; - case 'todo': - return ':fas_check-circle:'; - case 'tip': - case 'hint': - case 'important': - return ':fas_fire-alt:'; - case 'success': - case 'check': - case 'done': - return ':fas_check:'; - case 'question': - case 'help': - case 'faq': - return ':fas_question-circle:'; - case 'warning': - case 'caution': - case 'attention': - return ':fas_exclamation-triangle:'; - case 'failure': - case 'fail': - case 'missing': - return ':fas_times:'; - case 'danger': - case 'error': - return ':fas_bolt:'; - case 'bug': - return ':fas_bug:'; - case 'example': - return ':fas_list:'; - case 'quote': - case 'cite': - return ':fas_quote-left:'; + case "abstract": + case "summary": + case "tldr": + return ":fas_clipboard-list:"; + case "info": + return ":fas_info-circle:"; + case "todo": + return ":fas_check-circle:"; + case "tip": + case "hint": + case "important": + return ":fas_fire-alt:"; + case "success": + case "check": + case "done": + return ":fas_check:"; + case "question": + case "help": + case "faq": + return ":fas_question-circle:"; + case "warning": + case "caution": + case "attention": + return ":fas_exclamation-triangle:"; + case "failure": + case "fail": + case "missing": + return ":fas_times:"; + case "danger": + case "error": + return ":fas_bolt:"; + case "bug": + return ":fas_bug:"; + case "example": + return ":fas_list:"; + case "quote": + case "cite": + return ":fas_quote-left:"; default: - return ':fas_pencil-alt:'; + return ":fas_pencil-alt:"; } } } diff --git a/src/obsidian/processors/chartProcessor.ts b/src/obsidian/processors/chartProcessor.ts index efb3fc4..c279009 100644 --- a/src/obsidian/processors/chartProcessor.ts +++ b/src/obsidian/processors/chartProcessor.ts @@ -1,8 +1,8 @@ -import { Options } from '../../@types'; +import type { ChartJsOptions, Options } from "../../@types"; type ChartObject = { data: DataObject; - options: any; + options: ChartJsOptions; }; type DataEntry = { @@ -30,13 +30,13 @@ export class ChartProcessor { private useThemeColorsRegex = /(useThemeColors):\s(.*)/; private colorMap = [ - '#4285f4', - '#ea4335', - '#fbbc05', - '#34a853', - '#673ab7', - '#cccccc', - '#777777', + "#4285f4", + "#ea4335", + "#fbbc05", + "#34a853", + "#673ab7", + "#cccccc", + "#777777", ]; process(markdown: string, options: Options) { @@ -44,168 +44,160 @@ export class ChartProcessor { } transformChart(markdown: string, options: Options): string { - const startIdx = markdown.indexOf('```chart'); + const startIdx = markdown.indexOf("```chart"); if (startIdx < 0) { return markdown; - } else { - const endIdx = markdown.indexOf('```', startIdx + 11); - if (endIdx < 0) { - return markdown; + } + const endIdx = markdown.indexOf("```", startIdx + 11); + if (endIdx < 0) { + return markdown; + } + const colorMap = [...this.colorMap]; + + const before = markdown.substring(0, startIdx); + const after = markdown.substring(endIdx + 3); + const chartMarkup = markdown.substring(startIdx + 8, endIdx); + + if (this.typeRegex.test(chartMarkup)) { + const [, type] = this.typeRegex.exec(chartMarkup); + + const chart: ChartObject = { + data: { + datasets: [], + labels: [], + }, + options: { elements: {} }, + }; + + if (this.useThemeColorsRegex.test(chartMarkup)) { + const [, , value] = this.useThemeColorsRegex.exec(chartMarkup); + + if (value.trim() === "true") { + for (let i = 0; i < 7; i++) { + const style = getComputedStyle( + document.body, + ).getPropertyValue(`--chart-color-${i + 1}`); + if (style !== "") { + colorMap[i] = style; + } + } + } } - const colorMap = [...this.colorMap]; - - const before = markdown.substring(0, startIdx); - const after = markdown.substring(endIdx + 3); - const chartMarkup = markdown.substring(startIdx + 8, endIdx); + if (this.labelRegex.test(chartMarkup)) { + const [, labels] = this.labelRegex.exec(chartMarkup); + chart.data.labels = parseLabels(labels); - if (this.typeRegex.test(chartMarkup)) { - const [, type] = this.typeRegex.exec(chartMarkup); + this.datasetRegex.lastIndex = 0; - const chart: ChartObject = { - data: { - datasets: [], - labels: [], - }, - options: { elements: {} }, - }; + let i = 0; - if (this.useThemeColorsRegex.test(chartMarkup)) { - const [, , value] = - this.useThemeColorsRegex.exec(chartMarkup); - - if (value.trim() == 'true') { - for (let i = 0; i < 7; i++) { - const style = getComputedStyle( - document.body, - ).getPropertyValue('--chart-color-' + (i + 1)); - if (style != '') { - colorMap[i] = style; - } - } + while (true) { + const m = this.datasetRegex.exec(chartMarkup); + if (!m) { + break; + } + if (m.index === this.datasetRegex.lastIndex) { + this.datasetRegex.lastIndex++; } + const [, title, data] = m; + + chart.data.datasets.push({ + data: JSON.parse(data), + label: title, + backgroundColor: colorMap[i], + }); + i++; } - if (this.labelRegex.test(chartMarkup)) { - const [, labels] = this.labelRegex.exec(chartMarkup); - chart.data.labels = parseLabels(labels); - this.datasetRegex.lastIndex = 0; + chart.options.elements[type] = {}; - let i = 0; + if (this.spanGapsRegex.test(chartMarkup)) { + const [, key, value] = this.spanGapsRegex.exec(chartMarkup); + chart.options.elements[type][key] = JSON.parse(value); + } + if (this.tensionRegex.test(chartMarkup)) { + const [, key, value] = this.tensionRegex.exec(chartMarkup); + chart.options.elements[type][key] = JSON.parse(value); + } + if (this.beginAtZeroRegex.test(chartMarkup)) { + const [, key, value] = + this.beginAtZeroRegex.exec(chartMarkup); - let m; - while ((m = this.datasetRegex.exec(chartMarkup)) !== null) { - if (m.index === this.datasetRegex.lastIndex) { - this.datasetRegex.lastIndex++; - } - const [, title, data] = m; - - chart.data.datasets.push({ - data: JSON.parse(data), - label: title, - backgroundColor: colorMap[i], - }); - i++; + if (!chart.options.scales) { + chart.options.scales = {}; } - chart.options.elements[type] = {}; - - if (this.spanGapsRegex.test(chartMarkup)) { - const [, key, value] = - this.spanGapsRegex.exec(chartMarkup); - chart.options.elements[type][key] = JSON.parse(value); - } - if (this.tensionRegex.test(chartMarkup)) { - const [, key, value] = - this.tensionRegex.exec(chartMarkup); - chart.options.elements[type][key] = JSON.parse(value); + if (!chart.options.scales.y) { + chart.options.scales.y = {}; } - if (this.beginAtZeroRegex.test(chartMarkup)) { - const [, key, value] = - this.beginAtZeroRegex.exec(chartMarkup); - if (!chart.options.scales) { - chart.options.scales = {}; - } - - if (!chart.options.scales.y) { - chart.options.scales.y = {}; - } + chart.options.scales.y[key] = JSON.parse(value); + } + if (this.legendRegex.test(chartMarkup)) { + const [, , value] = this.legendRegex.exec(chartMarkup); - chart.options.scales.y[key] = JSON.parse(value); + if (!chart.options.plugins) { + chart.options.plugins = {}; } - if (this.legendRegex.test(chartMarkup)) { - const [, , value] = this.legendRegex.exec(chartMarkup); - if (!chart.options.plugins) { - chart.options.plugins = {}; - } - - if (!chart.options.plugins.legend) { - chart.options.plugins.legend = {}; - } - - chart.options.plugins.legend.display = - JSON.parse(value); + if (!chart.options.plugins.legend) { + chart.options.plugins.legend = {}; } - if (this.legendPositionRegex.test(chartMarkup)) { - const [, , value] = - this.legendPositionRegex.exec(chartMarkup); + chart.options.plugins.legend.display = JSON.parse(value); + } - if (!chart.options.plugins) { - chart.options.plugins = {}; - } + if (this.legendPositionRegex.test(chartMarkup)) { + const [, , value] = + this.legendPositionRegex.exec(chartMarkup); - if (!chart.options.plugins.legend) { - chart.options.plugins.legend = {}; - } + if (!chart.options.plugins) { + chart.options.plugins = {}; + } - chart.options.plugins.legend.position = value; + if (!chart.options.plugins.legend) { + chart.options.plugins.legend = {}; } - if (this.stackedRegex.test(chartMarkup)) { - const [, key, value] = - this.stackedRegex.exec(chartMarkup); - if (!chart.options.scales) { - chart.options.scales = {}; - } + chart.options.plugins.legend.position = value; + } + if (this.stackedRegex.test(chartMarkup)) { + const [, key, value] = this.stackedRegex.exec(chartMarkup); - if (!chart.options.scales.y) { - chart.options.scales.y = {}; - } - if (!chart.options.scales.x) { - chart.options.scales.x = {}; - } + if (!chart.options.scales) { + chart.options.scales = {}; + } - chart.options.scales.x[key] = JSON.parse(value); - chart.options.scales.y[key] = JSON.parse(value); + if (!chart.options.scales.y) { + chart.options.scales.y = {}; } - if (this.heightRegex.test(chartMarkup)) { - const [, , value] = this.heightRegex.exec(chartMarkup); - options.height = +value; + if (!chart.options.scales.x) { + chart.options.scales.x = {}; } - const canvas = `\n\n`; - - const result = - before.trimEnd() + - '\n' + - canvas + - '\n' + - after.trimStart(); - return this.transformChart(result, options); + chart.options.scales.x[key] = JSON.parse(value); + chart.options.scales.y[key] = JSON.parse(value); } + if (this.heightRegex.test(chartMarkup)) { + const [, , value] = this.heightRegex.exec(chartMarkup); + options.height = +value; + } + + const canvas = `\n\n`; + + const result = `${before.trimEnd()}\n${canvas}\n${after.trimStart()}`; + return this.transformChart(result, options); } - return markdown; } + return markdown; } } function parseLabels(labels: string): string[] { return labels .substring(1, labels.length - 1) - .split(',') - .map(label => { + .split(",") + .map((label) => { let value = label.trim(); if (value.startsWith("'") || value.startsWith('"')) { value = value.substring(1); diff --git a/src/obsidian/processors/commentProcessor.ts b/src/obsidian/processors/commentProcessor.ts index 410ec9c..1f05195 100644 --- a/src/obsidian/processors/commentProcessor.ts +++ b/src/obsidian/processors/commentProcessor.ts @@ -1,19 +1,18 @@ -import { CommentParser } from 'src/obsidian/comment'; +import { CommentParser } from "src/obsidian/comment"; export class CommentProcessor { private parser: CommentParser = new CommentParser(); process(markdown: string) { return markdown - .split('\n') - .map(line => { + .split("\n") + .map((line) => { const comment = this.parser.parseLine(line); if (comment) { return this.parser.replace(line, comment); - } else { - return line; } + return line; }) - .join('\n'); + .join("\n"); } } diff --git a/src/obsidian/processors/debugViewProcessor.ts b/src/obsidian/processors/debugViewProcessor.ts index 6414d03..1204109 100644 --- a/src/obsidian/processors/debugViewProcessor.ts +++ b/src/obsidian/processors/debugViewProcessor.ts @@ -1,4 +1,4 @@ -import { Options } from '../../@types'; +import type { Options } from "../../@types"; export class DebugViewProcessor { process(markdown: string, options: Options) { @@ -6,10 +6,10 @@ export class DebugViewProcessor { if (options.showGrid) { markdown - .split(new RegExp(options.separator, 'gmi')) + .split(new RegExp(options.separator, "gmi")) .map((slidegroup, index) => { return slidegroup - .split(new RegExp(options.verticalSeparator, 'gmi')) + .split(new RegExp(options.verticalSeparator, "gmi")) .map((slide, index) => { const [md, notes] = this.extractNotes( slide, @@ -18,7 +18,7 @@ export class DebugViewProcessor { let newSlide = this.addDebugCode(md); if (notes.length > 0) { - newSlide += '\n\n' + notes; + newSlide += `\n\n${notes}`; } output = output.replace(slide, newSlide); return newSlide; @@ -31,7 +31,7 @@ export class DebugViewProcessor { } addDebugCode(markdown: string) { - let gridBlock = ''; + let gridBlock = ""; gridBlock += '\n'; gridBlock += @@ -74,11 +74,11 @@ export class DebugViewProcessor { gridBlock += '\n'; - return markdown + '\n' + gridBlock; + return `${markdown}\n${gridBlock}`; } extractNotes(input: string, options: Options): [string, string] { - let noteSeparator = 'note:'; + let noteSeparator = "note:"; if (options.notesSeparator && options.notesSeparator.length > 0) { noteSeparator = options.notesSeparator; } @@ -86,8 +86,7 @@ export class DebugViewProcessor { const spliceIdx = input.indexOf(noteSeparator); if (spliceIdx > 0) { return [input.substring(0, spliceIdx), input.substring(spliceIdx)]; - } else { - return [input, '']; } + return [input, ""]; } } diff --git a/src/obsidian/processors/defaultBackgroundProcessor.ts b/src/obsidian/processors/defaultBackgroundProcessor.ts index a11466e..871d533 100644 --- a/src/obsidian/processors/defaultBackgroundProcessor.ts +++ b/src/obsidian/processors/defaultBackgroundProcessor.ts @@ -1,5 +1,5 @@ -import { CommentParser } from 'src/obsidian/comment'; -import { Options } from '../../@types'; +import { CommentParser } from "src/obsidian/comment"; +import type { Options } from "../../@types"; export class DefaultBackgroundProcessor { private slideCommentRegex = //; @@ -11,24 +11,23 @@ export class DefaultBackgroundProcessor { if (options?.bg) { if ( - options?.bg == 'transparent' || - options?.bg == 'rgba(0,0,0,0)' + options?.bg === "transparent" || + options?.bg === "rgba(0,0,0,0)" ) { - output = - ` -` + output; +${output}`; } markdown - .split(new RegExp(options.separator, 'gmi')) - .map(slidegroup => { + .split(new RegExp(options.separator, "gmi")) + .map((slidegroup) => { return slidegroup - .split(new RegExp(options.verticalSeparator, 'gmi')) - .map(slide => { + .split(new RegExp(options.verticalSeparator, "gmi")) + .map((slide) => { if (slide) { const newSlide = this.transformSlide( slide, @@ -36,9 +35,8 @@ body { ); output = output.split(slide).join(newSlide); return newSlide; - } else { - return slide; } + return slide; }) .join(options.verticalSeparator); }) @@ -52,17 +50,16 @@ body { const [match] = this.slideCommentRegex.exec(slide); const comment = this.parser.parseLine(match); if ( - !comment.hasAttribute('data-background-image') && - !comment.hasAttribute('data-background-color') + !comment.hasAttribute("data-background-image") && + !comment.hasAttribute("data-background-color") ) { - comment.addAttribute('bg', bg); + comment.addAttribute("bg", bg); } return slide.replace( this.slideCommentRegex, this.parser.commentToString(comment), ); - } else { - return slide + `\n\n`; } + return `${slide}\n\n`; } } diff --git a/src/obsidian/processors/dropProcessor.ts b/src/obsidian/processors/dropProcessor.ts index 9700339..499d73c 100644 --- a/src/obsidian/processors/dropProcessor.ts +++ b/src/obsidian/processors/dropProcessor.ts @@ -1,5 +1,5 @@ -import { CommentParser } from 'src/obsidian/comment'; -import { Options } from '../../@types'; +import { CommentParser } from "src/obsidian/comment"; +import type { Options } from "../../@types"; export class DropProcessor { private slideCommentRegex = //; @@ -10,18 +10,18 @@ export class DropProcessor { let output = markdown; markdown - .split(new RegExp(options.separator, 'gmi')) - .map(slidegroup => { + .split(new RegExp(options.separator, "gmi")) + .map((slidegroup) => { return slidegroup - .split(new RegExp(options.verticalSeparator, 'gmi')) - .map(slide => { + .split(new RegExp(options.verticalSeparator, "gmi")) + .map((slide) => { if (slide.trim().length > 0) { const newSlide = this.transformSlide( slide, options, ); const split = output.split(slide); - if (split.length == 2) { + if (split.length === 2) { output = split.join(newSlide); } else { const right = split.splice(1).join(slide); @@ -30,9 +30,8 @@ export class DropProcessor { output = both.join(newSlide); } return newSlide; - } else { - return slide; } + return slide; }) .join(options.verticalSeparator); }) @@ -44,14 +43,15 @@ export class DropProcessor { transformSlide(slide: string, options: Options) { const [md, notes] = this.extractNotes(slide, options); - let outMd, outSlideComment; + let outMd: string; + let outSlideComment: string; if (this.slideCommentRegex.test(md)) { const [match] = this.slideCommentRegex.exec(md); const comment = this.parser.parseLine(match); - comment.addClass('drop'); + comment.addClass("drop"); - outMd = md.replace(this.slideCommentRegex, ''); + outMd = md.replace(this.slideCommentRegex, ""); outMd = outMd.trim(); outSlideComment = this.parser.commentToString(comment); @@ -63,13 +63,13 @@ export class DropProcessor { let out = `${outSlideComment}\n\n${outMd}\n`; if (notes.length > 0) { - out += '\n\n' + notes; + out += `\n\n${notes}`; } return out; } extractNotes(input: string, options: Options): [string, string] { - let noteSeparator = 'note:'; + let noteSeparator = "note:"; if (options.notesSeparator && options.notesSeparator.length > 0) { noteSeparator = options.notesSeparator; } @@ -77,8 +77,7 @@ export class DropProcessor { const spliceIdx = input.indexOf(noteSeparator); if (spliceIdx > 0) { return [input.substring(0, spliceIdx), input.substring(spliceIdx)]; - } else { - return [input, '']; } + return [input, ""]; } } diff --git a/src/obsidian/processors/excalidrawProcessor.ts b/src/obsidian/processors/excalidrawProcessor.ts index d5d3802..81e3a22 100644 --- a/src/obsidian/processors/excalidrawProcessor.ts +++ b/src/obsidian/processors/excalidrawProcessor.ts @@ -1,5 +1,5 @@ import { Notice } from 'obsidian'; -import { ObsidianUtils } from '../obsidianUtils'; +import type { ObsidianUtils } from "../obsidianUtils"; export class ExcalidrawProcessor { private excalidrawImageRegex = @@ -33,6 +33,6 @@ export class ExcalidrawProcessor { ); return line; } - return `![[${imgFile}${ext == undefined ? '' : '|' + ext}]] ${comment ?? ''}`; + return `![[${imgFile}${ext === undefined ? "" : `|${ext}`}]] ${comment ?? ""}`; } } diff --git a/src/obsidian/processors/footNoteProcessor.ts b/src/obsidian/processors/footNoteProcessor.ts index d201bd3..394678d 100644 --- a/src/obsidian/processors/footNoteProcessor.ts +++ b/src/obsidian/processors/footNoteProcessor.ts @@ -1,4 +1,4 @@ -import { Options } from '../../@types'; +import type { Options } from "../../@types"; export class FootnoteProcessor { private regex = /\[\^([^\]]*)]/im; @@ -7,10 +7,10 @@ export class FootnoteProcessor { let output = markdown; markdown - .split(new RegExp(options.separator, 'gmi')) + .split(new RegExp(options.separator, "gmi")) .map((slidegroup, index) => { return slidegroup - .split(new RegExp(options.verticalSeparator, 'gmi')) + .split(new RegExp(options.verticalSeparator, "gmi")) .map((slide, index) => { if (this.regex.test(slide)) { const newSlide = this.transformFootNotes(slide); @@ -35,64 +35,55 @@ export class FootnoteProcessor { const footNotes = new Map(); - let reResult: RegExpExecArray; - while ((reResult = this.regex.exec(input))) { + while (true) { + const reResult = this.regex.exec(input); + if (!reResult) { + break; + } input = input - .split('\n') + .split("\n") .map((line, index) => { if (line.includes(reResult[0])) { - if (line.includes(reResult[0] + ': ')) { + if (line.includes(`${reResult[0]}: `)) { if (!footNotes.has(reResult[1])) { footNotes.set( reResult[1], - line.split(reResult[0] + ': ')[1], + line.split(`${reResult[0]}: `)[1], ); } - return ''; - } else { - const split = line.split(reResult[0]); + return ""; + } + const split = line.split(reResult[0]); - let result = split[0].trim(); - result += - '' + - noteIdx + - ''; - result += split[1].trim(); + let result = split[0].trim(); + result += `${noteIdx}`; + result += split[1].trim(); - noteIdx = noteIdx + 1; + noteIdx = noteIdx + 1; - return result; - } + return result; } return line; }) - .join('\n'); + .join("\n"); } - let footNotesBlock = ''; + let footNotesBlock = ""; footNotesBlock += '
\n'; - footNotesBlock += '
    \n'; + footNotesBlock += "
      \n"; footNotes.forEach((value, key) => { - footNotesBlock += - '
    1. \n\n' + - value + - '\n\n

    2. '; + footNotesBlock += `
    3. \n\n${value}\n\n

    4. `; }); - footNotesBlock += '
    \n'; - footNotesBlock += '
\n'; + footNotesBlock += "\n"; + footNotesBlock += "\n"; - const footnotePlaceholder = '<%? footnotes %>'; + const footnotePlaceholder = "<%? footnotes %>"; if (input.includes(footnotePlaceholder)) { return input.replaceAll(footnotePlaceholder, footNotesBlock); - } else { - return input + '\n' + footNotesBlock; } + return `${input}\n${footNotesBlock}`; } } diff --git a/src/obsidian/processors/formatProcessor.ts b/src/obsidian/processors/formatProcessor.ts index ecfb3d9..aa69aa1 100644 --- a/src/obsidian/processors/formatProcessor.ts +++ b/src/obsidian/processors/formatProcessor.ts @@ -7,38 +7,36 @@ export class FormatProcessor { let insideCodeBlock = false; return markdown - .split('\n') - .map(line => { - if (line.indexOf('```') > -1) { + .split("\n") + .map((line) => { + if (line.indexOf("```") > -1) { insideCodeBlock = !insideCodeBlock; } if (insideCodeBlock) { return line; - } else { - const split = line.split('`'); - if (split.length > 1) { - for (let i = 0; i < split.length; i = i + 2) { - split[i] = split[i] - .replaceAll( - this.boldRegex, - (sub, args) => `**${args.trim()}**`, - ) - .replaceAll(this.markRegex, '$1') - .replaceAll(this.commentRegex, ''); - } - return split.join('`'); - } else { - return line + } + const split = line.split("`"); + if (split.length > 1) { + for (let i = 0; i < split.length; i = i + 2) { + split[i] = split[i] .replaceAll( this.boldRegex, (sub, args) => `**${args.trim()}**`, ) - .replaceAll(this.markRegex, '$1') - .replaceAll(this.commentRegex, ''); + .replaceAll(this.markRegex, "$1") + .replaceAll(this.commentRegex, ""); } + return split.join("`"); } + return line + .replaceAll( + this.boldRegex, + (sub, args) => `**${args.trim()}**`, + ) + .replaceAll(this.markRegex, "$1") + .replaceAll(this.commentRegex, ""); }) - .join('\n') - .replaceAll(this.commentRegex, ''); + .join("\n") + .replaceAll(this.commentRegex, ""); } } diff --git a/src/obsidian/processors/fragmentProcessor.ts b/src/obsidian/processors/fragmentProcessor.ts index 64d3e27..f69ddda 100644 --- a/src/obsidian/processors/fragmentProcessor.ts +++ b/src/obsidian/processors/fragmentProcessor.ts @@ -1,5 +1,5 @@ import { CommentParser } from '../comment'; -import { Options } from '../../@types'; +import type { Options } from "../../@types"; export class FragmentProcessor { private parser: CommentParser; @@ -76,13 +76,9 @@ export class FragmentProcessor { } // See here: https://github.com/hakimel/reveal.js/issues/1848. This makes sure that reveals work when dealing with formatting in the list (e.g. bold / italic / code, etc.) - const extra_replacement = - '­' + this.parser.commentToString(comment); - line = line.replace('+ ', '- ' + extra_replacement); - line = line.replaceAll( - this.orderedListRegex, - '1. ' + extra_replacement, - ); + const extra_replacement = `­${this.parser.commentToString(comment)}`; + line = line.replace("+ ", `- ${extra_replacement}`); + line = line.replaceAll(this.orderedListRegex, `1. ${extra_replacement}`); const output = line; return output; diff --git a/src/obsidian/processors/gridProcessor.ts b/src/obsidian/processors/gridProcessor.ts index 9dab4f7..e651709 100644 --- a/src/obsidian/processors/gridProcessor.ts +++ b/src/obsidian/processors/gridProcessor.ts @@ -1,5 +1,5 @@ import { Properties } from 'src/obsidian/transformers'; -import { Options } from '../../@types'; +import type { Options } from "../../@types"; export class GridProcessor { private gridBlockRegex = /<\s*grid(?:(?!()).)*<\/grid>/gs; @@ -18,8 +18,8 @@ export class GridProcessor { .map(slide => { if (this.gridBlockRegex.test(slide)) { let before = this.transformSlide(slide); - let after; - while (after != before) { + let after: string; + while (after !== before) { if (after) { before = after; } @@ -41,8 +41,11 @@ export class GridProcessor { const result: Map = new Map(); this.gridBlockRegex.lastIndex = 0; - let m; - while ((m = this.gridBlockRegex.exec(slide)) !== null) { + while (true) { + const m = this.gridBlockRegex.exec(slide); + if (!m) { + break; + } if (m.index === this.gridBlockRegex.lastIndex) { this.gridBlockRegex.lastIndex++; } @@ -70,8 +73,11 @@ export class GridProcessor { const result: Map = new Map(); this.gridPropertiesRegex.lastIndex = 0; - let m; - while ((m = this.gridPropertiesRegex.exec(attributes)) !== null) { + while (true) { + const m = this.gridPropertiesRegex.exec(attributes); + if (!m) { + break; + } if (m.index === this.gridPropertiesRegex.lastIndex) { this.gridPropertiesRegex.lastIndex++; } diff --git a/src/obsidian/processors/iconsProcessor.ts b/src/obsidian/processors/iconsProcessor.ts index 1f5709a..c5431ad 100644 --- a/src/obsidian/processors/iconsProcessor.ts +++ b/src/obsidian/processors/iconsProcessor.ts @@ -6,7 +6,7 @@ export class IconsProcessor { } transformIconShortcode(markdown: string) { - markdown = markdown.replaceAll(this.regex, `![]($1 fa-$2)`); + markdown = markdown.replaceAll(this.regex, "![]($1 fa-$2)"); return markdown; } } diff --git a/src/obsidian/processors/internalLinkProcessor.ts b/src/obsidian/processors/internalLinkProcessor.ts index 46424e1..5eed85d 100644 --- a/src/obsidian/processors/internalLinkProcessor.ts +++ b/src/obsidian/processors/internalLinkProcessor.ts @@ -1,5 +1,5 @@ -import { ObsidianUtils } from '../obsidianUtils'; -import { Options } from '../../@types'; +import type { ObsidianUtils } from "../obsidianUtils"; +import type { Options } from "../../@types"; export class InternalLinkProcessor { private utils: ObsidianUtils; @@ -14,11 +14,10 @@ export class InternalLinkProcessor { if (options.enableLinks) { return markdown.replaceAll(this.regex, (sub, first, second) => { return `[${second}](obsidian://open?vault=${encodeURI(this.utils.vaultName)}&file=${encodeURI( - first == undefined ? second : first, + first === undefined ? second : first, )})`; }); - } else { - return markdown.replaceAll(this.regex, `$2`); } + return markdown.replaceAll(this.regex, "$2"); } } diff --git a/src/obsidian/processors/latexProcessor.ts b/src/obsidian/processors/latexProcessor.ts index b015288..d7879ba 100644 --- a/src/obsidian/processors/latexProcessor.ts +++ b/src/obsidian/processors/latexProcessor.ts @@ -12,15 +12,14 @@ export class LatexProcessor { if (match) { return ( - this.transformLatex(markdown.substring(0, match['index'])) + + this.transformLatex(markdown.substring(0, match.index)) + match[0] + this.skipCodeBlocks( - markdown.substring(match['index'] + match[0].length), + markdown.substring(match.index + match[0].length), ) ); - } else { - return this.transformLatex(markdown); } + return this.transformLatex(markdown); } private transformLatex(markdown: string) { @@ -41,49 +40,48 @@ export class LatexProcessor { return ( markdown //Escaped $ signs - .replaceAll('\\$', '~~d~~') + .replaceAll("\\$", "~~d~~") //Multiline in backticks - .replaceAll(/`\$\$/gm, '~~s~~') - .replaceAll(/\$\$`/gm, '~~e~~') + .replaceAll(/`\$\$/gm, "~~s~~") + .replaceAll(/\$\$`/gm, "~~e~~") //Singleline in backticks - .replaceAll(/`\$/gm, '~~ss~~') - .replaceAll(/\$`/gm, '~~se~~') + .replaceAll(/`\$/gm, "~~ss~~") + .replaceAll(/\$`/gm, "~~se~~") ); } private unmarkEscapedCharacters(markdown: string) { return markdown - .replaceAll('~~d~~', '\\$') - .replaceAll('~~e~~', '$$$$`') - .replaceAll('~~s~~', '`$$$$') - .replaceAll('~~ss~~', '`$') - .replaceAll('~~se~~', '$$`'); + .replaceAll("~~d~~", "\\$") + .replaceAll("~~e~~", "$$$$`") + .replaceAll("~~s~~", "`$$$$") + .replaceAll("~~ss~~", "`$") + .replaceAll("~~se~~", "$$`"); } private processSingleLine(markdown: string) { return markdown - .split('\n') - .map(line => { - if (line.includes('$')) { - line = line.replaceAll(this.singleLine, '`$$$1$$`'); + .split("\n") + .map((line) => { + if (line.includes("$")) { + line = line.replaceAll(this.singleLine, "`$$$1$$`"); } return line; }) - .join('\n'); + .join("\n"); } private processMultiLine(markdown: string) { - if (markdown.includes('$$')) { + if (markdown.includes("$$")) { return markdown - .split('$$') + .split("$$") .map((line, index) => { if (this.isOdd(index)) { return line; - } else { - return '`' + line + '`'; } + return `\`${line}\``; }) - .join('$$') + .join("$$") .slice(1, -1); } return markdown; diff --git a/src/obsidian/processors/mediaProcessor.ts b/src/obsidian/processors/mediaProcessor.ts index aec86a2..c201c62 100644 --- a/src/obsidian/processors/mediaProcessor.ts +++ b/src/obsidian/processors/mediaProcessor.ts @@ -1,6 +1,6 @@ import { isIcon, isImage, isUrl, isVideo, mimeTypeFor } from 'src/util'; import { CommentParser } from '../comment'; -import { ObsidianUtils } from '../obsidianUtils'; +import type { ObsidianUtils } from "../obsidianUtils"; export class MediaProcessor { private utils: ObsidianUtils; @@ -47,16 +47,17 @@ export class MediaProcessor { : this.parser.buildComment('element'); if (ext) { - let width, height; - if (ext.includes('x')) { - [width, height] = ext.split('x'); + let width: string; + let height: string; + if (ext.includes("x")) { + [width, height] = ext.split("x"); } else { width = ext; } - comment.addStyle('width', `${width}px`); + comment.addStyle("width", `${width}px`); if (height) { - comment.addStyle('height', `${height}px`); + comment.addStyle("height", `${height}px`); } } return this.parser.commentToString(comment); @@ -66,10 +67,12 @@ export class MediaProcessor { let result = ''; let lastIndex = 0; - let m; this.markdownMediaRegex.lastIndex = 0; - - while ((m = this.markdownMediaRegex.exec(line)) !== null) { + while (true) { + const m = this.markdownMediaRegex.exec(line); + if (!m) { + break; + } if (m.index === this.markdownMediaRegex.lastIndex) { this.markdownMediaRegex.lastIndex++; } @@ -100,11 +103,11 @@ export class MediaProcessor { if (line.match(/^(?:[ ]{4,}|\t)!\[/)) { // leading indent, leave as-is - embed = ' '; + embed = " "; } - let update = ''; - if (embed === '!' && (icon || image || video)) { + let update = ""; + if (embed === "!" && (icon || image || video)) { update = this.createImageElement(filePath, alt, commentString); } else { update = this.updateMarkdownLink( diff --git a/src/obsidian/processors/mermaidProcessor.ts b/src/obsidian/processors/mermaidProcessor.ts index eff45d4..97b75b9 100644 --- a/src/obsidian/processors/mermaidProcessor.ts +++ b/src/obsidian/processors/mermaidProcessor.ts @@ -4,32 +4,22 @@ export class MermaidProcessor { } transformMermaid(markdown: string): string { - const startIdx = markdown.indexOf('```mermaid'); + const startIdx = markdown.indexOf("```mermaid"); if (startIdx < 0) { return markdown; - } else { - const endIdx = markdown.indexOf('```', startIdx + 11); - if (endIdx < 0) { - return markdown; - } + } + const endIdx = markdown.indexOf("```", startIdx + 11); + if (endIdx < 0) { + return markdown; + } - const before = markdown.substring(0, startIdx); - const after = markdown.substring(endIdx + 3); - const content = markdown.substring(startIdx + 11, endIdx); + const before = markdown.substring(0, startIdx); + const after = markdown.substring(endIdx + 3); + const content = markdown.substring(startIdx + 11, endIdx); - const result = - before + - '\n' + - '
' + - '\n' + - content + - '\n' + - '
' + - '\n' + - after; + const result = `${before}\n
\n${content}\n
\n${after}`; - return this.transformMermaid(result); - } + return this.transformMermaid(result); } } diff --git a/src/obsidian/processors/multipleFileProcessor.ts b/src/obsidian/processors/multipleFileProcessor.ts index 0c3e572..e4b1b45 100644 --- a/src/obsidian/processors/multipleFileProcessor.ts +++ b/src/obsidian/processors/multipleFileProcessor.ts @@ -1,5 +1,5 @@ -import { isIcon, isImage, isUrl, isVideo } from 'src/util'; -import { ObsidianUtils } from '../obsidianUtils'; +import { isIcon, isImage, isUrl, isVideo } from "src/util"; +import type { ObsidianUtils } from "../obsidianUtils"; export class MultipleFileProcessor { private utils: ObsidianUtils; @@ -17,7 +17,7 @@ export class MultipleFileProcessor { process(markdown: string): string { return markdown - .split('\n') + .split("\n") .map((line, index) => { // replace file:// with /local-file-url in markdown links line = line.replace( @@ -32,7 +32,7 @@ export class MultipleFileProcessor { line = line.replace( this.wikilinkFileRegex, (_, filePath, aliasWithPipe) => { - const alias = aliasWithPipe ? aliasWithPipe : ''; + const alias = aliasWithPipe ? aliasWithPipe : ""; const transformedPath = this.transformAbsoluteFilePath(filePath); return `[[${transformedPath}${alias}]]`; @@ -47,19 +47,19 @@ export class MultipleFileProcessor { } return line; }) - .join('\n'); + .join("\n"); } private transformContent(line: string, match: RegExp) { - let comment = ''; - if (line.includes('/; @@ -10,11 +10,11 @@ export class SkipSlideProcessor { let output = markdown; markdown - .split(new RegExp(options.separator, 'gmi')) + .split(new RegExp(options.separator, "gmi")) .map((slidegroup, index) => { return slidegroup - .split(new RegExp(options.verticalSeparator, 'gmi')) - .map(slide => { + .split(new RegExp(options.verticalSeparator, "gmi")) + .map((slide) => { let newSlide = slide; if (this.slideCommentRegex.test(slide)) { @@ -22,8 +22,8 @@ export class SkipSlideProcessor { const comment = this.parser.parseLine(match); if ( - comment.hasAttribute('skip') && - comment.getAttribute('skip') == 'true' + comment.hasAttribute("skip") && + comment.getAttribute("skip") === "true" ) { newSlide = ''; diff --git a/src/obsidian/processors/templateProcessor.ts b/src/obsidian/processors/templateProcessor.ts index 61005e7..b789377 100644 --- a/src/obsidian/processors/templateProcessor.ts +++ b/src/obsidian/processors/templateProcessor.ts @@ -1,8 +1,8 @@ -import { CommentParser } from 'src/obsidian/comment'; -import { ObsidianUtils } from 'src/obsidian/obsidianUtils'; -import { FootnoteProcessor } from './footNoteProcessor'; -import { MultipleFileProcessor } from './multipleFileProcessor'; -import { Options } from '../../@types'; +import { CommentParser } from "src/obsidian/comment"; +import type { ObsidianUtils } from "src/obsidian/obsidianUtils"; +import { FootnoteProcessor } from "./footNoteProcessor"; +import { MultipleFileProcessor } from "./multipleFileProcessor"; +import type { Options } from "../../@types"; export class TemplateProcessor { private multipleFileProcessor: MultipleFileProcessor; @@ -32,19 +32,19 @@ export class TemplateProcessor { if (options.defaultTemplate != null) { markdown - .split(new RegExp(options.separator, 'gmi')) - .map(slidegroup => { + .split(new RegExp(options.separator, "gmi")) + .map((slidegroup) => { return slidegroup - .split(new RegExp(options.verticalSeparator, 'gmi')) - .map(slide => { + .split(new RegExp(options.verticalSeparator, "gmi")) + .map((slide) => { if (this.slideCommentRegex.test(slide)) { const [slideAnnotation] = this.slideCommentRegex.exec(slide); const comment = this.parser.parseLine(slideAnnotation); - if (!comment.hasAttribute('template')) { + if (!comment.hasAttribute("template")) { comment.addAttribute( - 'template', + "template", options.defaultTemplate, false, ); @@ -64,8 +64,7 @@ export class TemplateProcessor { input = input .split(slide) .join( - `\n` + - slide, + `\n${slide}`, ); } return slide; @@ -78,11 +77,11 @@ export class TemplateProcessor { let output = input; input - .split(new RegExp(options.separator, 'gmi')) - .map(slidegroup => { + .split(new RegExp(options.separator, "gmi")) + .map((slidegroup) => { return slidegroup - .split(new RegExp(options.verticalSeparator, 'gmi')) - .map(slide => { + .split(new RegExp(options.verticalSeparator, "gmi")) + .map((slide) => { if (this.templateCommentRegex.test(slide)) { try { // eslint-disable-next-line prefer-const @@ -98,25 +97,25 @@ export class TemplateProcessor { if (circuitCounter > 9) { console.warn( - 'WARNING: Circuit in template hierarchy detected!', + "WARNING: Circuit in template hierarchy detected!", ); break; } } md = md.replaceAll( this.emptySlideCommentRegex, - '', + "", ); md = md.trim(); md = this.computeVariables(md, options); if (notes.length > 0) { - md += '\n\n' + notes; + md += `\n\n${notes}`; } output = output.split(slide).join(md); return md; } catch (error) { console.error( - 'Cannot process template: ' + error, + `Cannot process template: ${error}`, ); return slide; } @@ -130,7 +129,7 @@ export class TemplateProcessor { } extractNotes(input: string, options: Options): [string, string] { - let noteSeparator = 'note:'; + let noteSeparator = "note:"; if (options.notesSeparator && options.notesSeparator.length > 0) { noteSeparator = options.notesSeparator; } @@ -138,9 +137,8 @@ export class TemplateProcessor { const spliceIdx = input.indexOf(noteSeparator); if (spliceIdx > 0) { return [input.substring(0, spliceIdx), input.substring(spliceIdx)]; - } else { - return [input, '']; } + return [input, ""]; } transformSlide(slide: string) { @@ -148,27 +146,29 @@ export class TemplateProcessor { const [, templateProperty, file] = this.templateCommentRegex.exec(slide); let fileWithExtension = file; - if (!fileWithExtension.endsWith('.md')) { - fileWithExtension = fileWithExtension + '.md'; + if (!fileWithExtension.endsWith(".md")) { + fileWithExtension = `${fileWithExtension}.md`; } let templateContent = this.utils.parseFile(fileWithExtension, null); templateContent = this.multipleFileProcessor.process(templateContent); templateContent = templateContent - .split('<% content %>') - .join(slide.replaceAll(templateProperty, '')); + .split("<% content %>") + .join(slide.replaceAll(templateProperty, "")); return templateContent; - } else { - return slide; } + return slide; } computeVariables(slide: string, options: Options): string { let result = slide; this.propertyRegex.lastIndex = 0; - let m; - while ((m = this.propertyRegex.exec(slide)) !== null) { + while (true) { + const m = this.propertyRegex.exec(slide); + if (m == null) { + break; + } if (m.index === this.propertyRegex.lastIndex) { this.propertyRegex.lastIndex++; } @@ -176,38 +176,45 @@ export class TemplateProcessor { // eslint-disable-next-line prefer-const let [match, name, content] = m; - if (name.includes('