Skip to content

Commit

Permalink
细节更新
Browse files Browse the repository at this point in the history
  • Loading branch information
riophae committed May 22, 2019
1 parent 28adb6e commit ee8def1
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/version-history/parseVersionHistory.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import splitLines from 'split-lines'
import stringWidth from 'string-width'

const MAX_CONTINOUS_BLANK_LINES = 1
const SUMMARY_LENGTH_LIMIT = 120
const MAX_CONTINOUS_BLANK_LINE_NUMBER = 1
const MAX_SUMMARY_LENGTH = 120

export default rawVersionHistory => {
const lines = splitLines(rawVersionHistory)
const parsed = []
let currentLine
let continousBlankLines = 0
let current
let continousBlankLineNumber = 0
let currentVersionItem

while (lines.length) {
// 保留每行行首的空格
currentLine = lines.shift().trimEnd()
currentLine = lines.shift().trim()

// 跳过空行
if (!currentLine) {
if (++continousBlankLines > MAX_CONTINOUS_BLANK_LINES) {
throw new Error(`连续空行不该超过 ${MAX_CONTINOUS_BLANK_LINES} 行`)
if (++continousBlankLineNumber > MAX_CONTINOUS_BLANK_LINE_NUMBER) {
throw new Error(`连续空行不该超过 ${MAX_CONTINOUS_BLANK_LINE_NUMBER} 行`)
} else {
continue
}
}
continousBlankLines = 0
continousBlankLineNumber = 0

// 注释
if (currentLine.startsWith('#')) {
Expand All @@ -33,12 +33,13 @@ export default rawVersionHistory => {
if (currentLine.startsWith('v')) {
const versionTag = currentLine.substr(1)

current = {
currentVersionItem = {
version: versionTag,
releaseDate: '0000-00-00',
summary: '',
updateDetails: [],
}
parsed.push(current)
parsed.push(currentVersionItem)

continue
}
Expand All @@ -47,7 +48,7 @@ export default rawVersionHistory => {
if (currentLine.startsWith('@')) {
const releaseDate = currentLine.substr(1)

current.releaseDate = releaseDate
currentVersionItem.releaseDate = releaseDate

continue
}
Expand All @@ -56,11 +57,11 @@ export default rawVersionHistory => {
if (currentLine.startsWith('~ ')) {
const summary = currentLine.substr(2)

if (summary.length > SUMMARY_LENGTH_LIMIT) {
throw new Error(`摘要不该长于 ${SUMMARY_LENGTH_LIMIT} 字`)
if (stringWidth(summary) > MAX_SUMMARY_LENGTH) {
throw new Error(`摘要不该长于 ${MAX_SUMMARY_LENGTH} 字`)
}

current.summary = summary
currentVersionItem.summary = summary

continue
}
Expand All @@ -69,7 +70,7 @@ export default rawVersionHistory => {
if (currentLine.startsWith('- ')) {
const updateDetail = currentLine.substr(2)

current.updateDetails.push(updateDetail)
currentVersionItem.updateDetails.push(updateDetail)

continue
}
Expand Down

0 comments on commit ee8def1

Please sign in to comment.