-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): add wordLimit support (#219)
- Loading branch information
1 parent
ccf1e6b
commit 97c47cb
Showing
12 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
presets: ['@babel/preset-env'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const { resolve } = require('path'); | ||
|
||
module.exports = { | ||
rootDir: resolve(__dirname), | ||
collectCoverage: true, | ||
transform: { | ||
'\\.js$': 'babel-jest', | ||
}, | ||
testMatch: ['<rootDir>/packages/**/__tests__/**/*.spec.js'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { getWords, getChinese, getWordNumber } from '../src/utils/wordCount'; | ||
|
||
describe('Words test', () => { | ||
it('Should count emplty content correctly', () => { | ||
expect(getWordNumber('')).toEqual(0); | ||
}); | ||
it('Should count english words correctly', () => { | ||
expect( | ||
getWordNumber( | ||
'A simple comment system with backend support fork from Valine' | ||
) | ||
).toEqual(10); | ||
}); | ||
|
||
it('Should pick chinese words correctly', () => { | ||
const chineseWords = getChinese( | ||
'Waline - 一款从 Valine 衍生的带后端评论系统。可以将 Waline 等价成 With backend Valine.' | ||
); | ||
|
||
expect(chineseWords.join('')).toEqual( | ||
'一款从衍生的带后端评论系统可以将等价成' | ||
); | ||
}); | ||
|
||
it('Should count word correctly', () => { | ||
expect( | ||
getWordNumber( | ||
'A simple comment system, with backend support fork from Valine.' | ||
) | ||
).toEqual(10); | ||
|
||
expect( | ||
getWordNumber( | ||
'Waline - 一款从 Valine 衍生的带后端评论系统。可以将 Waline 等价成 With backend Valine.' | ||
) | ||
).toEqual(25); | ||
}); | ||
|
||
it('Should omit other characters in Markdown', () => { | ||
expect(getWordNumber('#$%^\t&*% /?=+[\n{|}]\r')).toEqual(0); | ||
|
||
expect( | ||
getWordNumber( | ||
'\nA simple comment system,\n\n with _backend support_ fork from **Valine**.\n' | ||
) | ||
).toEqual(10); | ||
|
||
expect( | ||
getWordNumber( | ||
'Waline - 一款从 **Valine** 衍生的带后端评论系统。\n\n可以将 Waline 等价成 _With backend Valine_.' | ||
) | ||
).toEqual(25); | ||
}); | ||
|
||
it('Addtional counts with Markdown links and images', () => { | ||
const linkAddress = | ||
'//cdn.jsdelivr.net/npm/@waline/client/dist/Waline.min.js'; | ||
const linkMarkdown = `You can found Waline [here](${linkAddress}).`; | ||
const imageMarkdown = `Here is a image.\n\n![Alt](https://a/fake/link)`; | ||
|
||
const linkWords = getWords(linkAddress) | ||
.map((word) => word.trim()) | ||
.filter((word) => word); | ||
|
||
expect(linkWords).toEqual([ | ||
'cdn', | ||
'jsdelivr', | ||
'net', | ||
'npm', | ||
'waline', | ||
'client', | ||
'dist', | ||
'Waline', | ||
'min', | ||
'js', | ||
]); | ||
|
||
expect(getWordNumber(linkAddress)).toEqual(10); | ||
expect(getWordNumber(linkMarkdown)).toEqual(15); | ||
expect(getWordNumber(imageMarkdown)).toEqual(9); | ||
}); | ||
|
||
it('Can count code block', () => { | ||
const codeBlock = ` | ||
\`\`\`html | ||
<head> | ||
.. | ||
<script src="//cdn.jsdelivr.net/npm/@waline/client/dist/Waline.min.js"></script> | ||
... | ||
</head> | ||
<body> | ||
... | ||
<div id="waline"></div> | ||
<script> | ||
new Waline({ | ||
el: '#waline', | ||
path: location.pathname, | ||
serverURL: 'https://your-domain.vercel.app', | ||
}); | ||
</script> | ||
</body> | ||
\`\`\` | ||
`; | ||
|
||
const codeBlockwords = getWords(codeBlock) | ||
.map((word) => word.trim()) | ||
.filter((word) => word); | ||
|
||
expect(codeBlockwords).toEqual([ | ||
'html', | ||
'head', | ||
'script src', | ||
'cdn', | ||
'jsdelivr', | ||
'net', | ||
'npm', | ||
'waline', | ||
'client', | ||
'dist', | ||
'Waline', | ||
'min', | ||
'js', | ||
'script', | ||
'head', | ||
'body', | ||
'div id', | ||
'waline', | ||
'div', | ||
'script', | ||
'new Waline', | ||
'el', | ||
'waline', | ||
'path', | ||
'location', | ||
'pathname', | ||
'serverURL', | ||
'https', | ||
'your', | ||
'domain', | ||
'vercel', | ||
'app', | ||
'script', | ||
'body', | ||
]); | ||
|
||
expect(getWordNumber(codeBlock)).toEqual(37); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* The wordCount module should be lightweight as it's packed into client. | ||
* | ||
* So We just make a simple implement here | ||
* | ||
* Forked from https://github.com/vuepress-theme-hope/vuepress-theme-hope/blob/v1/packages/reading-time/src/node/reading-time.ts | ||
*/ | ||
|
||
export const getWords = (content) => | ||
content.match(/[\w\d\s\u00C0-\u024F]+/giu) || []; | ||
|
||
export const getChinese = (content) => content.match(/[\u4E00-\u9FA5]/gu) || []; | ||
|
||
export const getWordNumber = (content) => | ||
getWords(content).reduce( | ||
(accumulator, word) => | ||
accumulator + (word.trim() === '' ? 0 : word.trim().split(/\s+/u).length), | ||
0 | ||
) + getChinese(content).length; |