Skip to content

Commit

Permalink
[changelog-generator] improve GitHubReleaseMarkdown output
Browse files Browse the repository at this point in the history
& add Preview Channel description
  • Loading branch information
chr56 committed Apr 10, 2024
1 parent aac8bb4 commit 6dd3150
Showing 1 changed file with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
package util.phonograph.output

import util.phonograph.dateString
import util.phonograph.releasenote.Language
import util.phonograph.releasenote.Notes
import util.phonograph.releasenote.ReleaseChannel
import util.phonograph.releasenote.ReleaseNote
import java.io.Writer

abstract class Markdown : OutputFormat {

protected fun border(text: String) = "**$text**"

protected fun title(text: String, level: Int) = "${"#".repeat(level)} $text"
protected fun title(text: String, level: Int) = "${"#".repeat(level)} $text\n"

protected fun makeUnorderedList(items: List<String>) = buildString {
for (item in items) {
Expand All @@ -26,14 +28,26 @@ abstract class Markdown : OutputFormat {
appendLine("${index + 1}. $item")
}
}

protected fun subtitle(language: Language, previewWarning: Boolean): String = buildString {
when (language) {
Language.EN -> {
if (previewWarning) appendLine("This is a _Preview Channel_ Release (with package name suffix `preview`), which might have potential bugs.")
}

Language.ZH -> {
if (previewWarning) appendLine("此为预览通道版本 (包名后缀`preview`), 可能存在潜在问题!")
}
}
}
}


class GitHubReleaseMarkdown(private val releaseNote: ReleaseNote) : Markdown() {

@Suppress("SameParameterValue")
private fun section(note: Notes.Note, title: String, level: Int): String = buildString {
appendLine(title(title, level)).append('\n')
appendLine(title(title, level))
if (note.highlights.isNotEmpty()) appendLine(makeUnorderedList(note.highlights)).append('\n')
if (note.items.isNotEmpty()) appendLine(makeOrderedList(note.items)).append('\n')
}
Expand All @@ -42,14 +56,22 @@ class GitHubReleaseMarkdown(private val releaseNote: ReleaseNote) : Markdown() {
override fun write(target: Writer) {

val title = title(border("v${releaseNote.version} ${dateString(releaseNote.timestamp)}"), 2)
val en = section(releaseNote.notes.en, "EN", 3)
val zh = section(releaseNote.notes.zh, "ZH", 3)

val subtitleEN = subtitle(Language.EN, releaseNote.channel == ReleaseChannel.PREVIEW)
val subtitleZH = subtitle(Language.ZH, releaseNote.channel == ReleaseChannel.PREVIEW)
val contentEN = section(releaseNote.notes.en, "EN", 3)
val contentZH = section(releaseNote.notes.zh, "ZH", 3)
val extra = "**Commit log**: "

target.append(title).append('\n').append('\n')
target.append(en).append('\n').append('\n')
target.append(zh).append('\n').append('\n')
target.append(extra).append('\n').append('\n')
target.append(title)
target.append('\n')
target.append(subtitleEN)
target.append(subtitleZH)
target.append('\n')
target.append(contentEN)
target.append(contentZH)
target.append('\n')
target.append(extra)

}
}
Expand Down

0 comments on commit 6dd3150

Please sign in to comment.