Skip to content

Commit

Permalink
feat: add varied style support
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidurber committed Feb 2, 2022
1 parent a4a03de commit f5afc17
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ It's really simple to use, just add a code block to your document:
title: string (default: undefined)
allow_inconsistent_headings: boolean (default: false)
delimiter: string (default: |)
varied_style: boolean (default: false)
```
````

Expand Down Expand Up @@ -69,6 +70,20 @@ Inline styles render the highest level of heading such as H2 `## Heading 2`, you

![](media/inline-headings.jpg)

### Varied Style

Varied style allows for setting the topmost level of your headings, and the rest of the levels to the opposite style.

For example if you have `varied_style` set to true and your list style is bullet, the first level will be bullet and the subsequent headings will be number style.

**Style: Bullet**

![](media/varied-style-bullet.jpg)

**Style: Number**

![](media/varied-style-number.jpg)

### External Rendering Support

![](media/settings.jpg)
Expand Down
Binary file added media/varied-style-bullet.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/varied-style-number.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/settings-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ export class DynamicTOCSettingsTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Enable varied style")
.setDesc(
"Varied style allows for the most top level heading to match your list style, then subsequent levels to be the opposite. For example if your list style is number, then your level 2 headings will be number, any levels lower then 2 will be bullet and vice versa."
)
.addToggle((cb) =>
cb.setValue(this.plugin.settings.varied_style).onChange(async (val) => {
this.plugin.settings.varied_style = val;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Delimiter")
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface TableOptions {
title?: string;
allow_inconsistent_headings: boolean;
delimiter?: string;
varied_style?: boolean;
}

export const EXTERNAL_MARKDOWN_PREVIEW_STYLE = {
Expand Down
26 changes: 24 additions & 2 deletions src/utils/__tests__/__snapshots__/extract-headings.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@ exports[`Extract headings build markdown text should match snapshot 1`] = `
"1. [[#foo]]
1. [[#bar]]
1. [[#baz]]
1. [[#Something Alt Text|Alt Text]]"
1. [[#Something Alt Text|Alt Text]]
1. [[#level 1]]
1. [[#level 1 a]]"
`;

exports[`Extract headings build markdown text should match snapshot when varied_style is true and style is bullet 1`] = `
"- [[#foo]]
1. [[#bar]]
1. [[#baz]]
1. [[#Something Alt Text|Alt Text]]
- [[#level 1]]
1. [[#level 1 a]]"
`;

exports[`Extract headings build markdown text should match snapshot when varied_style is true and style is number 1`] = `
"1. [[#foo]]
- [[#bar]]
- [[#baz]]
- [[#Something Alt Text|Alt Text]]
1. [[#level 1]]
- [[#level 1 a]]"
`;

exports[`Extract headings build markdown text should match snapshot with inconsistent heading levels 1`] = `
Expand All @@ -19,5 +39,7 @@ exports[`Extract headings build markdown text should match snapshot with title 1
1. [[#foo]]
1. [[#bar]]
1. [[#baz]]
1. [[#Something Alt Text|Alt Text]]"
1. [[#Something Alt Text|Alt Text]]
1. [[#level 1]]
1. [[#level 1 a]]"
`;
27 changes: 27 additions & 0 deletions src/utils/__tests__/extract-headings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ describe("Extract headings", () => {
heading: "[[Something|Alt Text]]",
level: 4,
},
{
heading: "level 1",
level: 1,
},
{
heading: "level 1 a",
level: 2,
},
],
} as CachedMetadata;
it("should match snapshot", () => {
Expand All @@ -33,6 +41,25 @@ describe("Extract headings", () => {
expect(extractHeadings(defaultHeadings, options)).toMatchSnapshot();
});

it("should match snapshot when varied_style is true and style is bullet", () => {
const options = {
max_depth: 4,
min_depth: 1,
style: "bullet",
varied_style: true,
} as TableOptions;
expect(extractHeadings(defaultHeadings, options)).toMatchSnapshot();
});
it("should match snapshot when varied_style is true and style is number", () => {
const options = {
max_depth: 4,
min_depth: 1,
style: "number",
varied_style: true,
} as TableOptions;
expect(extractHeadings(defaultHeadings, options)).toMatchSnapshot();
});

it("should match snapshot with title", () => {
const options = {
max_depth: 4,
Expand Down
16 changes: 15 additions & 1 deletion src/utils/extract-headings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ export function extractHeadings(
return buildMarkdownText(headingInstances, options);
}

function getIndicator(
heading: Heading,
firstLevel: number,
options: TableOptions
) {
const defaultIndicator = (options.style === "number" && "1.") || "-";
if (!options.varied_style) return defaultIndicator;
// if the heading is at the same level as the first heading and varied_style is true, then only set the first indicator to the selected style
if (heading.level === firstLevel) return defaultIndicator;
return options.style === "number" ? "-" : "1.";
}

/**
* Generate markdown for a standard table of contents
* @param headings - Array of heading instances
Expand All @@ -33,10 +45,12 @@ function buildMarkdownText(headings: Heading[], options: TableOptions): string {
if (options.title) {
list.push(`${options.title}`);
}

let previousIndent = 0;
for (let i = 0; i < headings.length; i++) {
const heading = headings[i];
const itemIndication = (options.style === "number" && "1.") || "-";

const itemIndication = getIndicator(heading, firstHeadingDepth, options);
let numIndents = new Array(Math.max(0, heading.level - firstHeadingDepth));

if (options.allow_inconsistent_headings) {
Expand Down

0 comments on commit f5afc17

Please sign in to comment.