mirror of
https://github.com/patruusbarba/ObsidianSpoilerPlugin.git
synced 2026-07-22 07:48:59 +00:00
Fix unwrap corrupting content that contains code fences
Unwrap no longer strips a surrounding ``` fence, so a selection whose content begins/ends with a code fence round-trips unchanged. Bump to 0.2.1. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
1de5a3756e
commit
394ec75a6f
5 changed files with 10 additions and 30 deletions
|
|
@ -39,7 +39,7 @@ to reveal the hidden contents.
|
|||
|
||||
### From a release (recommended)
|
||||
|
||||
1. Download `obsidian-spoiler-plugin_v0.2.zip` from the
|
||||
1. Download `obsidian-spoiler-plugin_v0.2.1.zip` from the
|
||||
[latest release](https://github.com/PatruusBarba/ObsidianSpoilerPlugin/releases/latest).
|
||||
2. Unzip it into your vault's plugins folder so you end up with
|
||||
`<vault>/.obsidian/plugins/obsidian-spoiler-plugin/` containing `main.js` and
|
||||
|
|
|
|||
31
main.ts
31
main.ts
|
|
@ -4,8 +4,6 @@ import { Editor, MarkdownView, Notice, Plugin } from "obsidian";
|
|||
const SPOILER_HEADER = /^>\s*\[!spoiler\]-?/i;
|
||||
/** Matches the header line once the leading callout marker (`> `) is stripped. */
|
||||
const SPOILER_HEADER_STRIPPED = /^\[!spoiler\]-?/i;
|
||||
/** Matches a fenced code block delimiter (``` optionally followed by a language). */
|
||||
const CODE_FENCE = /^```/;
|
||||
|
||||
/**
|
||||
* Returns true when the given selection already is a spoiler callout, i.e. its
|
||||
|
|
@ -35,10 +33,10 @@ export function wrapSpoiler(text: string): string {
|
|||
}
|
||||
|
||||
/**
|
||||
* Reverses {@link wrapSpoiler}: strips the callout markers, drops the
|
||||
* `[!spoiler]` header and returns the inner content. For backwards
|
||||
* compatibility it also strips a surrounding fenced code block if one is
|
||||
* present (the format used by earlier versions).
|
||||
* Reverses {@link wrapSpoiler}: removes the callout marker (`> `) from every
|
||||
* line and drops the `[!spoiler]` header, returning the inner content exactly
|
||||
* as it was before wrapping. The content itself is never altered, so a
|
||||
* selection that contains code fences (```) round-trips unchanged.
|
||||
*/
|
||||
export function unwrapSpoiler(text: string): string {
|
||||
// Remove the callout marker (`> ` or `>`) from every line.
|
||||
|
|
@ -52,27 +50,8 @@ export function unwrapSpoiler(text: string): string {
|
|||
break;
|
||||
}
|
||||
}
|
||||
let body = lines.slice(start);
|
||||
|
||||
// If the body is wrapped in a fenced code block, strip the fences.
|
||||
const first = body.findIndex((line) => line.trim() !== "");
|
||||
let last = -1;
|
||||
for (let i = body.length - 1; i >= 0; i--) {
|
||||
if (body[i].trim() !== "") {
|
||||
last = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (
|
||||
first !== -1 &&
|
||||
last > first &&
|
||||
CODE_FENCE.test(body[first].trim()) &&
|
||||
CODE_FENCE.test(body[last].trim())
|
||||
) {
|
||||
body = body.slice(first + 1, last);
|
||||
}
|
||||
|
||||
return body.join("\n");
|
||||
return lines.slice(start).join("\n");
|
||||
}
|
||||
|
||||
export default class SpoilerPlugin extends Plugin {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"id": "obsidian-spoiler-plugin",
|
||||
"name": "Spoiler",
|
||||
"version": "0.2.0",
|
||||
"version": "0.2.1",
|
||||
"minAppVersion": "0.15.0",
|
||||
"description": "Wrap selected text in a collapsible spoiler callout, or unwrap an existing spoiler, from a ribbon button.",
|
||||
"author": "PatruusBarba",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "obsidian-spoiler-plugin",
|
||||
"version": "0.2.0",
|
||||
"version": "0.2.1",
|
||||
"description": "Wrap selected text in a collapsible spoiler callout, or unwrap an existing spoiler, from a ribbon button.",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
{
|
||||
"1.0.0": "0.15.0",
|
||||
"0.2.0": "0.15.0"
|
||||
"0.2.0": "0.15.0",
|
||||
"0.2.1": "0.15.0"
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue