feat: support read the enabled status from the cssclasses field of property

Resolve: #5
This commit is contained in:
dragonish 2025-04-02 19:56:23 +08:00
parent 5941b848a8
commit ee3122c7d1
No known key found for this signature in database
GPG key ID: 6F42FA9E807A5177
3 changed files with 83 additions and 2 deletions

View file

@ -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
View file

@ -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);
}
}
}

View file

@ -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;
});
});