@docusaurus/utils
Advanced tools
Comparing version 2.0.0-alpha.54 to 2.0.0-alpha.55
@@ -46,2 +46,3 @@ /** | ||
export declare function getSubFolder(file: string, refDir: string): string | null; | ||
export declare function createExcerpt(fileString: string): string | undefined; | ||
export declare function parse(fileString: string): { | ||
@@ -48,0 +49,0 @@ frontMatter: { |
@@ -169,16 +169,48 @@ "use strict"; | ||
// Regex for an import statement. | ||
const importRegexString = '^(.*import){1}(.+){0,1}\\s[\'"](.+)[\'"];'; | ||
const importRegexString = '^(.*import){1}(.+){0,1}\\s[\'"](.+)[\'"];?'; | ||
function createExcerpt(fileString) { | ||
let fileContent = fileString.trimLeft(); | ||
if (RegExp(importRegexString).test(fileContent)) { | ||
fileContent = fileContent | ||
.replace(RegExp(importRegexString, 'gm'), '') | ||
.trimLeft(); | ||
} | ||
const fileLines = fileContent.split('\n'); | ||
for (let fileLine of fileLines) { | ||
const cleanedLine = fileLine | ||
// Remove HTML tags. | ||
.replace(/<[^>]*>/g, '') | ||
// Remove ATX-style headers. | ||
.replace(/^\#{1,6}\s*([^#]*)\s*(\#{1,6})?/gm, '$1') | ||
// Remove emphasis and strikethroughs. | ||
.replace(/([\*_~]{1,3})(\S.*?\S{0,1})\1/g, '$2') | ||
// 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. | ||
.replace(/\s?(:(::|[^:\n])+:)/g, '') | ||
.trim(); | ||
if (cleanedLine) { | ||
return cleanedLine; | ||
} | ||
} | ||
return undefined; | ||
} | ||
exports.createExcerpt = createExcerpt; | ||
function parse(fileString) { | ||
const options = { | ||
excerpt: (file) => { | ||
let fileContent = file.content.trimLeft(); | ||
// Hacky way of stripping out import statements from the excerpt | ||
// TODO: Find a better way to do so, possibly by compiling the Markdown content, | ||
// stripping out HTML tags and obtaining the first line. | ||
if (RegExp(importRegexString).test(fileContent)) { | ||
fileContent = fileContent | ||
.replace(RegExp(importRegexString, 'gm'), '') | ||
.trimLeft(); | ||
} | ||
file.excerpt = fileContent.split('\n', 1).shift(); | ||
file.excerpt = createExcerpt(file.content); | ||
}, | ||
@@ -185,0 +217,0 @@ }; |
{ | ||
"name": "@docusaurus/utils", | ||
"version": "2.0.0-alpha.54", | ||
"version": "2.0.0-alpha.55", | ||
"description": "Node utility functions for Docusaurus packages", | ||
@@ -24,3 +24,3 @@ "main": "./lib/index.js", | ||
}, | ||
"gitHead": "753d117025935834af856f4751d0051911254d7b" | ||
"gitHead": "d94a549cfbf054acf80e92fda0fbe8072f8e1d9d" | ||
} |
@@ -20,2 +20,3 @@ /** | ||
aliasedSitePath, | ||
createExcerpt, | ||
} from '../index'; | ||
@@ -296,2 +297,58 @@ | ||
}); | ||
test('createExcerpt', () => { | ||
const asserts = [ | ||
// Regular content | ||
{ | ||
input: ` | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ex urna, molestie et sagittis ut, varius ac justo. | ||
Nunc porttitor libero nec vulputate venenatis. Nam nec rhoncus mauris. Morbi tempus est et nibh maximus, tempus venenatis arcu lobortis. | ||
`, | ||
output: | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ex urna, molestie et sagittis ut, varius ac justo.', | ||
}, | ||
// Content with imports declarations and Markdown markup, as well as Emoji | ||
{ | ||
input: ` | ||
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:. | ||
Nunc porttitor libero nec vulputate venenatis. Nam nec rhoncus mauris. Morbi tempus est et nibh maximus, tempus venenatis arcu lobortis. | ||
`, | ||
output: | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ex urna, molestie et sagittis ut, varius ac justo.', | ||
}, | ||
// Content beginning with admonitions | ||
{ | ||
input: ` | ||
import Component from '@site/src/components/Component' | ||
:::caution | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
::: | ||
Nunc porttitor libero nec vulputate venenatis. Nam nec rhoncus mauris. Morbi tempus est et nibh maximus, tempus venenatis arcu lobortis. | ||
`, | ||
output: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', | ||
}, | ||
// Content beginning with heading | ||
{ | ||
input: ` | ||
## Lorem ipsum dolor sit amet | ||
Nunc porttitor libero nec vulputate venenatis. Nam nec rhoncus mauris. Morbi tempus est et nibh maximus, tempus venenatis arcu lobortis. | ||
`, | ||
output: 'Lorem ipsum dolor sit amet', | ||
}, | ||
]; | ||
asserts.forEach((testCase) => { | ||
expect(createExcerpt(testCase.input)).toEqual(testCase.output); | ||
}); | ||
}); | ||
}); |
@@ -186,4 +186,47 @@ /** | ||
// Regex for an import statement. | ||
const importRegexString = '^(.*import){1}(.+){0,1}\\s[\'"](.+)[\'"];'; | ||
const importRegexString = '^(.*import){1}(.+){0,1}\\s[\'"](.+)[\'"];?'; | ||
export function createExcerpt(fileString: string): string | undefined { | ||
let fileContent = fileString.trimLeft(); | ||
if (RegExp(importRegexString).test(fileContent)) { | ||
fileContent = fileContent | ||
.replace(RegExp(importRegexString, 'gm'), '') | ||
.trimLeft(); | ||
} | ||
const fileLines = fileContent.split('\n'); | ||
for (let fileLine of fileLines) { | ||
const cleanedLine = fileLine | ||
// Remove HTML tags. | ||
.replace(/<[^>]*>/g, '') | ||
// Remove ATX-style headers. | ||
.replace(/^\#{1,6}\s*([^#]*)\s*(\#{1,6})?/gm, '$1') | ||
// Remove emphasis and strikethroughs. | ||
.replace(/([\*_~]{1,3})(\S.*?\S{0,1})\1/g, '$2') | ||
// 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. | ||
.replace(/\s?(:(::|[^:\n])+:)/g, '') | ||
.trim(); | ||
if (cleanedLine) { | ||
return cleanedLine; | ||
} | ||
} | ||
return undefined; | ||
} | ||
export function parse( | ||
@@ -200,14 +243,6 @@ fileString: string, | ||
excerpt: (file: matter.GrayMatterFile<string>): void => { | ||
let fileContent = file.content.trimLeft(); | ||
// Hacky way of stripping out import statements from the excerpt | ||
// TODO: Find a better way to do so, possibly by compiling the Markdown content, | ||
// stripping out HTML tags and obtaining the first line. | ||
if (RegExp(importRegexString).test(fileContent)) { | ||
fileContent = fileContent | ||
.replace(RegExp(importRegexString, 'gm'), '') | ||
.trimLeft(); | ||
} | ||
file.excerpt = fileContent.split('\n', 1).shift(); | ||
file.excerpt = createExcerpt(file.content); | ||
}, | ||
@@ -214,0 +249,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1225735
11
965