Skip to content

Commit

Permalink
fix(v2): strip images and footnotes for excerpt correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lex111 committed May 31, 2020
1 parent 0047db2 commit 56ba00b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
16 changes: 15 additions & 1 deletion packages/docusaurus-utils/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ describe('load utils', () => {
import Component from '@site/src/components/Component';
import Component from '@site/src/components/Component'
Lorem **ipsum** dolor sit \`amet\`, consectetur _adipiscing_ elit. [**Vestibulum**](https://wiktionary.org/wiki/vestibulum) ex urna, ~molestie~ et sagittis ut, varius ac justo :wink:.
Lorem **ipsum** dolor sit \`amet\`[^1], consectetur _adipiscing_ elit. [**Vestibulum**](https://wiktionary.org/wiki/vestibulum) ex urna[^bignote], ~molestie~ et sagittis ut, varius ac justo :wink:.
Nunc porttitor libero nec vulputate venenatis. Nam nec rhoncus mauris. Morbi tempus est et nibh maximus, tempus venenatis arcu lobortis.
`,
Expand Down Expand Up @@ -343,6 +343,20 @@ describe('load utils', () => {
`,
output: 'Lorem ipsum dolor sit amet',
},
// Content beginning with blockquote
{
input: `
> Lorem ipsum dolor sit amet
`,
output: 'Lorem ipsum dolor sit amet',
},
// Content beginning with image (eg. blog post)
{
input: `
![Lorem ipsum](/img/lorem-ipsum.svg)
`,
output: 'Lorem ipsum',
},
];

asserts.forEach((testCase) => {
Expand Down
8 changes: 4 additions & 4 deletions packages/docusaurus-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,16 @@ export function createExcerpt(fileString: string): string | undefined {
.replace(/^\#{1,6}\s*([^#]*)\s*(\#{1,6})?/gm, '$1')
// Remove emphasis and strikethroughs.
.replace(/([\*_~]{1,3})(\S.*?\S{0,1})\1/g, '$2')
// Remove images.
.replace(/\!\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
// Remove footnotes.
.replace(/\[\^.+?\](\: .*?$)?/g, '')
// Remove inline links.
.replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
// Remove inline code.
.replace(/`(.+?)`/g, '$1')
// Remove images.
.replace(/\!\[(.*?)\][\[\(].*?[\]\)]/g, '')
// Remove blockquotes.
.replace(/^\s{0,3}>\s?/g, '')
// Remove footnotes.
.replace(/\[\^.+?\](\: .*?$)?/g, '')
// Remove admonition definition.
.replace(/(:{3}.*)/, '')
// Remove Emoji names within colons include preceding whitespace.
Expand Down

0 comments on commit 56ba00b

Please sign in to comment.