1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 09:46:22 +00:00

Merge pull request #398 from asmsuechan/iss-397

Fix note title
This commit is contained in:
Kazz Yokomizo
2017-11-09 21:05:14 +09:00
committed by GitHub
4 changed files with 89 additions and 31 deletions

View File

@@ -3,6 +3,7 @@ import emoji from 'markdown-it-emoji'
import math from '@rokt33r/markdown-it-math' import math from '@rokt33r/markdown-it-math'
import _ from 'lodash' import _ from 'lodash'
// FIXME We should not depend on global variable.
const katex = window.katex const katex = window.katex
function createGutter (str) { function createGutter (str) {
@@ -137,37 +138,9 @@ md.renderer.render = function render (tokens, options, env) {
let result = originalRender.call(md.renderer, tokens, options, env) let result = originalRender.call(md.renderer, tokens, options, env)
return result return result
} }
// FIXME We should not depend on global variable.
window.md = md window.md = md
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(/([\*_]{1,3})(\S.*?\S)\1/g, '$2')
.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
}
function normalizeLinkText (linkText) { function normalizeLinkText (linkText) {
return md.normalizeLinkText(linkText) return md.normalizeLinkText(linkText)
} }
@@ -178,7 +151,7 @@ const markdown = {
const renderedContent = md.render(content) const renderedContent = md.render(content)
return renderedContent return renderedContent
}, },
strip,
normalizeLinkText normalizeLinkText
} }
export default markdown export default markdown

View File

@@ -0,0 +1,39 @@
/**
* @fileoverview Text trimmer for markdown note.
*/
/**
* @param {string} input
* @return {string}
*/
export function strip (input) {
let 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

@@ -9,7 +9,7 @@ import FolderSelect from './FolderSelect'
import dataApi from 'browser/main/lib/dataApi' import dataApi from 'browser/main/lib/dataApi'
import { hashHistory } from 'react-router' import { hashHistory } from 'react-router'
import ee from 'browser/main/lib/eventEmitter' import ee from 'browser/main/lib/eventEmitter'
import markdown from 'browser/lib/markdown' import markdown from 'browser/lib/markdownTextHelper'
import StatusBar from '../StatusBar' import StatusBar from '../StatusBar'
import _ from 'lodash' import _ from 'lodash'
import { findNoteTitle } from 'browser/lib/findNoteTitle' import { findNoteTitle } from 'browser/lib/findNoteTitle'

View File

@@ -0,0 +1,46 @@
/**
* @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'],
// Don't remove underscore
['`MY_TITLE`', 'MY_TITLE'],
['MY_TITLE', 'MY_TITLE'],
// I have no idea for it...
['```test', '`test']
]
testCases.forEach(testCase => {
const [input, expected] = testCase
t.is(markdown.strip(input), expected, `Test for strip() input: ${input} expected: ${expected}`)
})
})