diff --git a/README.md b/README.md index 4877f07..de82739 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,4 @@ If you want to combine various formatting options, you can nest them, but make s *||This text will be hidden and italicized||* ``` -To reveal or hide a spoiler, click on it. You may opt to always show all spoilers by enabling the "Always show spoilers" setting. +To reveal or hide a spoiler, click on it. You may opt to always show all spoilers by enabling the **Always show spoilers** setting. diff --git a/main.ts b/main.ts index 6c884b3..e36dee6 100644 --- a/main.ts +++ b/main.ts @@ -14,18 +14,26 @@ const updateReadingMode = (element: HTMLElement, plugin: InlineSpoilerPlugin) => const allowedElems = element.findAll("p, li, h1, h2, h3, h4, h5, h6, blockquote, em, strong, b, i, a, th, td"); for (const elem of allowedElems) { - let newHTML = elem.innerHTML; + // Split the text content of the element by the spoiler pattern, keeping the delimiters + const parts = elem.innerText.split(/(\|\|[^|]+\|\|)/g); - // find all substrings that start and end with the string "||" - const matches = elem.innerText.match(SPOILER_REGEX); + // Clear the element's content + while (elem.firstChild) { + elem.removeChild(elem.firstChild); + } - if (matches) { - for (const match of matches) { - const spoilerSpan = createSpan({ cls: "inline_spoilers-spoiler", text: match.slice(2, -2) }); - newHTML = newHTML.replace(match, spoilerSpan.outerHTML); + // Process each part + for (const part of parts) { + if (SPOILER_REGEX.test(part)) { + // It's a spoiler, create a span for it + const spoilerText = part.slice(2, -2); // Remove the || delimiters + const spoilerSpan = createSpan({ cls: "inline_spoilers-spoiler", text: spoilerText }); + elem.appendChild(spoilerSpan); + } else { + // It's regular text, create a text node for it + const textNode = document.createTextNode(part); + elem.appendChild(textNode); } - - elem.innerHTML = newHTML; } } @@ -74,17 +82,17 @@ export default class InlineSpoilerPlugin extends Plugin { // remove all spoilers const spoilers = Array.from(this.app.workspace.containerEl.querySelectorAll(".inline_spoilers-spoiler")) as HTMLElement[]; for (const spoiler of spoilers) { - spoiler.outerHTML = `||${spoiler.innerText}||`; + const parent = spoiler.parentNode; + const spoilerText = document.createTextNode(`||${spoiler.innerText}||`); + if (parent) { + parent.replaceChild(spoilerText, spoiler); + } } } async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); - if (this.settings.showAllSpoilers) { - this.app.workspace.containerEl.classList.add("inline_spoilers-revealed"); - } else { - this.app.workspace.containerEl.classList.remove("inline_spoilers-revealed"); - } + this.app.workspace.containerEl.toggleClass("inline_spoilers-revealed", this.settings.showAllSpoilers); } async saveSettings() { @@ -112,13 +120,7 @@ class InlineSpoilerPluginSettingsTab extends PluginSettingTab { .setValue(this.plugin.settings.showAllSpoilers) .onChange(async (value) => { this.plugin.settings.showAllSpoilers = value; - - if (value) { - this.app.workspace.containerEl.classList.add("inline_spoilers-revealed"); - } else { - this.app.workspace.containerEl.classList.remove("inline_spoilers-revealed"); - } - + this.app.workspace.containerEl.toggleClass("inline_spoilers-revealed", value); await this.plugin.saveSettings(); })); } diff --git a/manifest.json b/manifest.json index b0478ae..746aea2 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "inline-spoilers", "name": "Inline spoilers", - "version": "1.0.1", + "version": "1.0.2", "minAppVersion": "1.6.5", "description": "Adds Discord-like syntax for inline spoilers in reader mode.", "author": "logonoff", diff --git a/package-lock.json b/package-lock.json index fc7cdbe..cdb0a77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,15 @@ { - "name": "obsidian-sample-plugin", - "version": "1.0.0", + "name": "obsidian-inline-spoilers", + "version": "1.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "obsidian-sample-plugin", - "version": "1.0.0", + "name": "obsidian-inline-spoilers", + "version": "1.0.2", "license": "MIT", - "dependencies": { - "@codemirror/language": "^6.10.2" - }, "devDependencies": { + "@codemirror/language": "^6.10.2", "@codemirror/view": "^6.28.4", "@types/node": "^16.11.6", "@typescript-eslint/eslint-plugin": "5.29.0", @@ -27,6 +25,7 @@ "version": "6.10.2", "resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.10.2.tgz", "integrity": "sha512-kgbTYTo0Au6dCSc/TFy7fK3fpJmgHDv1sG1KNQKJXVi+xBTEeBPY/M30YXiU6mMXeH+YIDLsbrT4ZwNRdtF+SA==", + "dev": true, "dependencies": { "@codemirror/state": "^6.0.0", "@codemirror/view": "^6.23.0", @@ -39,12 +38,14 @@ "node_modules/@codemirror/state": { "version": "6.4.1", "resolved": "https://registry.npmjs.org/@codemirror/state/-/state-6.4.1.tgz", - "integrity": "sha512-QkEyUiLhsJoZkbumGZlswmAhA7CBU02Wrz7zvH4SrcifbsqwlXShVXg65f3v/ts57W3dqyamEriMhij1Z3Zz4A==" + "integrity": "sha512-QkEyUiLhsJoZkbumGZlswmAhA7CBU02Wrz7zvH4SrcifbsqwlXShVXg65f3v/ts57W3dqyamEriMhij1Z3Zz4A==", + "dev": true }, "node_modules/@codemirror/view": { "version": "6.28.4", "resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.28.4.tgz", "integrity": "sha512-QScv95fiviSQ/CaVGflxAvvvDy/9wi0RFyDl4LkHHWiMr/UPebyuTspmYSeN5Nx6eujcPYwsQzA6ZIZucKZVHQ==", + "dev": true, "dependencies": { "@codemirror/state": "^6.4.0", "style-mod": "^4.1.0", @@ -504,12 +505,14 @@ "node_modules/@lezer/common": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.2.1.tgz", - "integrity": "sha512-yemX0ZD2xS/73llMZIK6KplkjIjf2EvAHcinDi/TfJ9hS25G0388+ClHt6/3but0oOxinTcQHJLDXh6w1crzFQ==" + "integrity": "sha512-yemX0ZD2xS/73llMZIK6KplkjIjf2EvAHcinDi/TfJ9hS25G0388+ClHt6/3but0oOxinTcQHJLDXh6w1crzFQ==", + "dev": true }, "node_modules/@lezer/highlight": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@lezer/highlight/-/highlight-1.2.0.tgz", "integrity": "sha512-WrS5Mw51sGrpqjlh3d4/fOwpEV2Hd3YOkp9DBt4k8XZQcoTHZFB7sx030A6OcahF4J1nDQAa3jXlTVVYH50IFA==", + "dev": true, "dependencies": { "@lezer/common": "^1.0.0" } @@ -518,6 +521,7 @@ "version": "1.4.1", "resolved": "https://registry.npmjs.org/@lezer/lr/-/lr-1.4.1.tgz", "integrity": "sha512-CHsKq8DMKBf9b3yXPDIU4DbH+ZJd/sJdYOW2llbW/HudP5u0VS6Bfq1hLYfgU7uAYGFIyGGQIsSOXGPEErZiJw==", + "dev": true, "dependencies": { "@lezer/common": "^1.0.0" } @@ -2078,7 +2082,8 @@ "node_modules/style-mod": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/style-mod/-/style-mod-4.1.2.tgz", - "integrity": "sha512-wnD1HyVqpJUI2+eKZ+eo1UwghftP6yuFheBqqe+bWCotBjC2K1YnteJILRMs3SM4V/0dLEW1SC27MWP5y+mwmw==" + "integrity": "sha512-wnD1HyVqpJUI2+eKZ+eo1UwghftP6yuFheBqqe+bWCotBjC2K1YnteJILRMs3SM4V/0dLEW1SC27MWP5y+mwmw==", + "dev": true }, "node_modules/supports-color": { "version": "7.2.0", @@ -2191,7 +2196,8 @@ "node_modules/w3c-keyname": { "version": "2.2.8", "resolved": "https://registry.npmjs.org/w3c-keyname/-/w3c-keyname-2.2.8.tgz", - "integrity": "sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ==" + "integrity": "sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ==", + "dev": true }, "node_modules/which": { "version": "2.0.2", diff --git a/package.json b/package.json index 802fc6c..5f201c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-inline-spoilers", - "version": "1.0.1", + "version": "1.0.2", "description": "Adds Discord-like syntax for inline spoilers in reader mode.", "main": "main.js", "scripts": { diff --git a/versions.json b/versions.json index b1e3de5..aa9078b 100644 --- a/versions.json +++ b/versions.json @@ -1,4 +1,5 @@ { + "1.0.2": "1.6.5", "1.0.1": "1.6.5", "1.0.0": "1.6.5" }