mirror of
https://github.com/dragonish/obsidian-heading-decorator.git
synced 2026-07-22 05:42:05 +00:00
feat: support read the enabled status from the cssclasses field of property
Resolve: #5
This commit is contained in:
parent
5941b848a8
commit
ee3122c7d1
3 changed files with 83 additions and 2 deletions
|
|
@ -254,3 +254,30 @@ export function getBoolean(value: unknown) {
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if css string contains enable/disable heading classes.
|
||||
*
|
||||
* @param cssString CSS string.
|
||||
* @param mode Mode to check for.
|
||||
* @returns true if css string contains enable-${mode}-heading, false if it contains disable-${mode}-heading, and null otherwise.
|
||||
*/
|
||||
export function checkEnabledCss(
|
||||
cssString: string,
|
||||
mode: HeadingMetadataSettingsType
|
||||
) {
|
||||
const cssClasses = cssString.split(" ").map((c) => c.trim());
|
||||
for (const cssClass of cssClasses) {
|
||||
switch (cssClass) {
|
||||
case `enable-${mode}-heading`:
|
||||
return true;
|
||||
case `disable-${mode}-heading`:
|
||||
return false;
|
||||
case "enable-heading":
|
||||
return true;
|
||||
case "disable-heading":
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return null; // default to null if no matching class is found
|
||||
}
|
||||
|
|
|
|||
18
main.ts
18
main.ts
|
|
@ -24,6 +24,7 @@ import {
|
|||
getOrderedCustomIdents,
|
||||
diffLevel,
|
||||
getBoolean,
|
||||
checkEnabledCss,
|
||||
} from "./common/data";
|
||||
import { Counter, Querier } from "./common/counter";
|
||||
import { Heading } from "./common/heading";
|
||||
|
|
@ -556,8 +557,23 @@ export default class HeadingPlugin extends Plugin {
|
|||
return (
|
||||
getBoolean(metadataSettings[mode]) ?? getBoolean(metadataSettings.all)
|
||||
);
|
||||
} else {
|
||||
}
|
||||
if (metadataSettings != null) {
|
||||
return getBoolean(metadataSettings);
|
||||
} else {
|
||||
const cssclasses = frontmatter.cssclasses;
|
||||
if (Array.isArray(cssclasses)) {
|
||||
for (const cssItem of cssclasses) {
|
||||
if (typeof cssItem === "string") {
|
||||
const s = checkEnabledCss(cssItem, mode);
|
||||
if (s != null) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (typeof cssclasses === "string") {
|
||||
return checkEnabledCss(cssclasses, mode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,11 @@
|
|||
import "mocha";
|
||||
import { expect } from "chai";
|
||||
import { diffLevel, compareMarkdownText, getBoolean } from "../../common/data";
|
||||
import {
|
||||
diffLevel,
|
||||
compareMarkdownText,
|
||||
getBoolean,
|
||||
checkEnabledCss,
|
||||
} from "../../common/data";
|
||||
|
||||
describe("common/data", function () {
|
||||
it("diffLevel", function () {
|
||||
|
|
@ -53,4 +58,37 @@ describe("common/data", function () {
|
|||
expect(getBoolean([])).to.be.null;
|
||||
expect(getBoolean({})).to.be.null;
|
||||
});
|
||||
|
||||
it("checkEnabledCss", function () {
|
||||
expect(checkEnabledCss("", "reading")).to.be.null;
|
||||
expect(checkEnabledCss("enable-preview-heading", "reading")).to.be.null;
|
||||
expect(checkEnabledCss("enable-heading", "reading")).to.be.true;
|
||||
expect(checkEnabledCss("disable-heading", "reading")).to.be.false;
|
||||
expect(checkEnabledCss("enable-reading-heading", "reading")).to.be.true;
|
||||
expect(checkEnabledCss("disable-reading-heading", "reading")).to.be.false;
|
||||
expect(checkEnabledCss("enable-heading disable-heading", "reading")).to.be
|
||||
.true;
|
||||
expect(checkEnabledCss("disable-heading enable-heading", "reading")).to.be
|
||||
.false;
|
||||
expect(
|
||||
checkEnabledCss(
|
||||
"enable-reading-heading disable-reading-heading",
|
||||
"reading"
|
||||
)
|
||||
).to.be.true;
|
||||
expect(
|
||||
checkEnabledCss(
|
||||
"disable-reading-heading enable-reading-heading",
|
||||
"reading"
|
||||
)
|
||||
).to.be.false;
|
||||
expect(checkEnabledCss("enable-heading disable-reading-heading", "reading"))
|
||||
.to.be.true;
|
||||
expect(checkEnabledCss("disable-heading enable-reading-heading", "reading"))
|
||||
.to.be.false;
|
||||
expect(checkEnabledCss("enable-reading-heading disable-heading", "reading"))
|
||||
.to.be.true;
|
||||
expect(checkEnabledCss("disable-reading-heading enable-heading", "reading"))
|
||||
.to.be.false;
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue