1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

add: cut out trimming text logic for md note title and add unit test

This commit is contained in:
sota1235
2017-04-22 12:35:22 +09:00
committed by asmsuechan
parent 26357bd4bc
commit 2ce96186f2
2 changed files with 80 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
/**
* @fileoverview Text trimmer for markdown note.
*/
/**
* @param {string} input
* @return {string}
*/
export function strip (input) {
var output = input
try {
output = output
.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm, '$1')
.replace(/\n={2,}/g, '\n')
.replace(/~~/g, '')
.replace(/`{3}.*\n/g, '')
.replace(/<(.*?)>/g, '$1')
.replace(/^[=\-]{2,}\s*$/g, '')
.replace(/\[\^.+?\](: .*?$)?/g, '')
.replace(/\s{0,2}\[.*?\]: .*?$/g, '')
.replace(/!\[.*?\][\[\(].*?[\]\)]/g, '')
.replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
.replace(/>/g, '')
.replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, '')
.replace(/^#{1,6}\s*([^#]*)\s*(#{1,6})?/gm, '$1')
.replace(/(`{3,})(.*?)\1/gm, '$2')
.replace(/^-{3,}\s*$/g, '')
.replace(/`(.+?)`/g, '$1')
.replace(/\n{2,}/g, '\n\n')
} catch (e) {
console.error(e)
return input
}
return output
}
export default {
strip,
}

View File

@@ -0,0 +1,41 @@
/**
* @fileoverview Unit test for browser/lib/markdown
*/
const test = require('ava')
const markdown = require('browser/lib/markdownTextHelper')
test(t => {
// [input, expected]
const testCases = [
// List
[' - ', ' '],
[' + ', ' '],
[' * ', ' '],
[' * ', ' '],
[' 1. ', ' '],
[' 2. ', ' '],
[' 10. ', ' '],
["\t- ", "\t"],
['- ', ''],
// Header with using line
["\n==", "\n"],
["\n===", "\n"],
["test\n===", "test\n"],
// Code block
["```test\n", ''],
["```test\nhoge", 'hoge'],
// HTML tag
['<>', ''],
['<test>', 'test'],
['hoge<test>', 'hogetest'],
['<test>moge', 'testmoge'],
// Emphasis
['~~', ''],
['~~text~~', 'text'],
]
testCases.forEach(testCase => {
const [input, expected] = testCase;
t.is(markdown.strip(input), expected, `Test for strip() input: ${input} expected: ${expected}`);
})
})