Skip to content

Commit

Permalink
fix: HTML title generated from heading should not have ruby text and …
Browse files Browse the repository at this point in the history
…HTML tags

- fix #166
  • Loading branch information
MurakamiShinyu committed Oct 13, 2023
1 parent 76721d2 commit e8df7e4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/plugins/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ const readTitleFromHeading = (tree: Node): string | undefined => {
heading.children = heading.children.filter(
(child: Node) => child.type !== 'footnote',
);
const text = toString(heading);
// Remove ruby text and HTML tags
const text = toString(heading)
.replace(/{(.+?)(?<=[^\\|])\|(.+?)}/g, '$1')
.replace(/<[^<>]*>/g, '');
heading.children = children;

return text;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/slug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const createSlug = (heading: Heading, slugger: GithubSlugger) => {
heading.children = heading.children.filter(
(child: Node) => child.type !== 'footnote',
);
const text = slugger.slug(toString(heading));
const text = slugger.slug(toString(heading).replace(/<[^<>]*>/g, ''));
heading.children = children;

return text;
Expand Down
8 changes: 5 additions & 3 deletions tests/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ other-meta2: 'other2'
],
[
{ name: 'rel', value: 'stylesheet' },
{ name: 'type', value: 'text/css'},
{ name: 'type', value: 'text/css' },
{ name: 'href', value: 'sample2.css' },
],
],
Expand Down Expand Up @@ -130,8 +130,10 @@ other-meta2: 'other2'
});

it('Title from heading', () => {
const received = readMetadata(`# Heading Title`);
const expected = 'Heading Title';
const received = readMetadata(
`# Heading Title with {Ruby|ルビ} and <mark>HTML tag</mark>`,
);
const expected = 'Heading Title with Ruby and HTML tag';

expect(received.title).toBe(expected);
});
Expand Down

0 comments on commit e8df7e4

Please sign in to comment.