Skip to content

Commit

Permalink
feat: Disable section with blockquote heading
Browse files Browse the repository at this point in the history
  • Loading branch information
akabekobeko committed Apr 30, 2021
1 parent 394be2f commit f493797
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
20 changes: 15 additions & 5 deletions docs/vfm.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,13 @@ ruby rt {

## Sectionization

<Badge type="warning">PRE-RELEASE</Badge>

If specify the attribute in the heading, it will be as follows.
Make the heading a hierarchical section.

- `id` is moved to `<section>`
- Other attributes are copied to `<section>`
- Do not sectionize if parent is `blockquote`.
- The attributes of the heading are basically copied to the section.
- The `id` attribute is moved to the section.
- The `hidden` attribute is not copied and only the heading applies.
- Set the `levelN` class in the section to match the heading depth.

**VFM**

Expand All @@ -212,6 +213,8 @@ If specify the attribute in the heading, it will be as follows.
# Level 1

## Level 2

> # Not Sectionize
```

**HTML**
Expand All @@ -235,6 +238,10 @@ If specify the attribute in the heading, it will be as follows.
<h2>Level 2</h2>
</section>
</section>

<blockquote>
<h1 id="not-sectionize">Not Sectionize</h1>
</blockquote>
```

**CSS**
Expand All @@ -255,6 +262,9 @@ section.title > h1:first-child {
}
.level2 {
}

blockquote > h1 {
}
```

### Plain section
Expand Down
13 changes: 11 additions & 2 deletions src/plugins/section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const checkProperties = (node: any, depth: number) => {

// {hidden} specifier
if (Object.keys(hProperties).includes('hidden')) {
node.data.hProperties.hidden = "hidden";
node.data.hProperties.hidden = 'hidden';
}

// output section levels like Pandoc
Expand All @@ -50,14 +50,23 @@ const checkProperties = (node: any, depth: number) => {

/**
* Wrap the header in sections.
* - Do not sectionize if parent is `blockquote`.
* - The attributes of the heading are basically copied to the section.
* - The `id` attribute is moved to the section.
* - The `hidden` attribute is not copied and only the heading applies.
* - Set the `levelN` class in the section to match the heading depth.
* @param node Node of Markdown AST.
* @param ancestors Parents.
* @todo handle `@subtitle` properly.
*/
const sectionize = (node: any, ancestors: Parent[]) => {
const parent = ancestors[ancestors.length - 1];
if (parent.type === 'blockquote') {
return;
}

const start = node;
const depth = start.depth;
const parent = ancestors[ancestors.length - 1];

const isEnd = (node: any) =>
(node.type === 'heading' && node.depth <= depth) || node.type === 'export';
Expand Down
11 changes: 11 additions & 0 deletions tests/section.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ it('Heading with hidden attribute', () => {
expect(received).toBe(expected);
});

it('Disable section with blockquote heading', () => {
const md = '> # Not Sectionize';
const received = stringify(md, { partial: true });
const expected = `
<blockquote>
<h1 id="not-sectionize">Not Sectionize</h1>
</blockquote>
`;
expect(received).toBe(expected);
});

it('<h7> is not heading', () => {
const md = '####### こんにちは {.test}';
const received = stringify(md, { partial: true, disableFormatHtml: true });
Expand Down

0 comments on commit f493797

Please sign in to comment.