From 729f022550b7976a1b2db712df1f67dc74659461 Mon Sep 17 00:00:00 2001 From: lowlighter <22963968+lowlighter@users.noreply.github.com> Date: Mon, 13 Sep 2021 15:17:27 -0400 Subject: [PATCH] fix(plugins/code): handle no code snippet case --- source/plugins/code/index.mjs | 19 +++---- source/templates/classic/partials/code.ejs | 61 +++++++++++++--------- 2 files changed, 46 insertions(+), 34 deletions(-) diff --git a/source/plugins/code/index.mjs b/source/plugins/code/index.mjs index 8f8e8027..8bc93800 100644 --- a/source/plugins/code/index.mjs +++ b/source/plugins/code/index.mjs @@ -45,16 +45,17 @@ export default async function({login, q, imports, data, rest, account}, {enabled const files = events .flatMap(({sha, commit:{message, url}, files}) => files.map(({filename, status, additions, deletions, patch}) => ({sha, message, filename, status, additions, deletions, patch, repo:url.match(/repos[/](?[\s\S]+)[/]git[/]commits/)?.groups?.repo}))) .filter(({patch}) => (patch ? (patch.match(/\n/mg)?.length ?? 1) : Infinity) < lines) - const snippet = files[Math.floor(Math.random()*files.length)] + const snippet = files[Math.floor(Math.random()*files.length)] ?? null + if (snippet) { + //Trim common indent from content and change line feed + if (!snippet.patch.split("\n").shift().endsWith("@@")) + snippet.patch = snippet.patch.replace(/^(?@@.*?@@)/, "$\n") + const indent = Math.min(...(snippet.patch.match(/^[+-]? +/mg)?.map(indent => (indent.length ?? Infinity) - indent.startsWith("+") - indent.startsWith("-")) ?? [])) || 0 + const content = imports.htmlescape(snippet.patch.replace(/\r\n/mg, "\n").replace(new RegExp(`^([+-]?)${" ".repeat(indent)}`, "mg"), "$1")) - //Trim common indent from content and change line feed - if (!snippet.patch.split("\n").shift().endsWith("@@")) - snippet.patch = snippet.patch.replace(/^(?@@.*?@@)/, "$\n") - const indent = Math.min(...(snippet.patch.match(/^[+-]? +/mg)?.map(indent => (indent.length ?? Infinity) - indent.startsWith("+") - indent.startsWith("-")) ?? [])) || 0 - const content = imports.htmlescape(snippet.patch.replace(/\r\n/mg, "\n").replace(new RegExp(`^([+-]?)${" ".repeat(indent)}`, "mg"), "$1")) - - //Format patch - snippet.patch = imports.htmlunescape((await imports.highlight(content, "diff")).trim()) + //Format patch + snippet.patch = imports.htmlunescape((await imports.highlight(content, "diff")).trim()) + } //Results return {snippet} diff --git a/source/templates/classic/partials/code.ejs b/source/templates/classic/partials/code.ejs index dbee7bd5..a82b386e 100644 --- a/source/templates/classic/partials/code.ejs +++ b/source/templates/classic/partials/code.ejs @@ -14,31 +14,42 @@ <% } else { %> -
-
-
- - From <%= plugins.code.snippet.repo %> -
-
- - <%= plugins.code.snippet.message %> -
-
- - <%= plugins.code.snippet.sha.substring(0, 8) %> - <%= plugins.code.snippet.filename %> - ++<%= plugins.code.snippet.additions %> --<%= plugins.code.snippet.deletions %> -
-
-
-
-
-
- <%- plugins.code.snippet.patch %> -
-
-
+ <% if (plugins.code.snippet) { %> +
+
+
+ + From <%= plugins.code.snippet.repo %> +
+
+ + <%= plugins.code.snippet.message %> +
+
+ + <%= plugins.code.snippet.sha.substring(0, 8) %> + <%= plugins.code.snippet.filename %> + ++<%= plugins.code.snippet.additions %> --<%= plugins.code.snippet.deletions %> +
+
+
+
+
+
+ <%- plugins.code.snippet.patch %> +
+
+
+ <% } else { %> +
+
+
+ + No recent code snippet found +
+
+
+ <% } %> <% } %> <% } %>