fix(plugins/code): handle no code snippet case

This commit is contained in:
lowlighter
2021-09-13 15:17:27 -04:00
parent 9ee422b4fc
commit 729f022550
2 changed files with 46 additions and 34 deletions

View File

@@ -45,8 +45,8 @@ 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[/](?<repo>[\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(/^(?<coord>@@.*?@@)/, "$<coord>\n")
@@ -55,6 +55,7 @@ export default async function({login, q, imports, data, rest, account}, {enabled
//Format patch
snippet.patch = imports.htmlunescape((await imports.highlight(content, "diff")).trim())
}
//Results
return {snippet}

View File

@@ -14,6 +14,7 @@
</section>
</div>
<% } else { %>
<% if (plugins.code.snippet) { %>
<div class="row">
<section class="largeable-column-fields">
<div class="field">
@@ -39,6 +40,16 @@
</div>
</section>
</div>
<% } else { %>
<div class="row">
<section class="largeable-column-fields">
<div class="field">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M8 1.5a6.5 6.5 0 100 13 6.5 6.5 0 000-13zM0 8a8 8 0 1116 0A8 8 0 010 8zm6.5-.25A.75.75 0 017.25 7h1a.75.75 0 01.75.75v2.75h.25a.75.75 0 010 1.5h-2a.75.75 0 010-1.5h.25v-2h-.25a.75.75 0 01-.75-.75zM8 6a1 1 0 100-2 1 1 0 000 2z"></path></svg>
No recent code snippet found
</div>
</section>
</div>
<% } %>
<% } %>
</section>
<% } %>