Skip to content

Commit 13fc826

Browse files
authored
feat(plugins/code): improvements (lowlighter#891) [skip ci]
1 parent c344622 commit 13fc826

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

source/plugins/code/index.mjs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export default async function({login, q, imports, data, rest, account}, {enabled
1515
}
1616

1717
//Load inputs
18-
let {load, lines, visibility, languages, skipped} = imports.metadata.plugins.code.inputs({data, q, account})
18+
let {load, days, lines, visibility, languages, skipped} = imports.metadata.plugins.code.inputs({data, q, account})
19+
if (!days)
20+
days = Infinity
1921
skipped.push(...data.shared["repositories.skipped"])
2022
const pages = Math.ceil(load / 100)
2123

@@ -35,7 +37,8 @@ export default async function({login, q, imports, data, rest, account}, {enabled
3537
.filter(({actor}) => account === "organization" ? true : actor.login?.toLocaleLowerCase() === login.toLocaleLowerCase())
3638
.filter(({repo:{name:repo}}) => !((skipped.includes(repo.split("/").pop())) || (skipped.includes(repo))))
3739
.filter(event => visibility === "public" ? event.public : true)
38-
.flatMap(({payload}) => Promise.all(payload.commits.map(async commit => (await rest.request(commit.url)).data))),
40+
.filter(({created_at}) => Number.isFinite(days) ? new Date(created_at) > new Date(Date.now() - days * 24 * 60 * 60 * 1000) : true)
41+
.flatMap(({created_at:created, payload}) => Promise.all(payload.commits.map(async commit => ({created:new Date(created), ...(await rest.request(commit.url)).data})))),
3942
]),
4043
]
4144
.flat()
@@ -51,7 +54,7 @@ export default async function({login, q, imports, data, rest, account}, {enabled
5154

5255
//Search for a random snippet
5356
let files = events
54-
.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})))
57+
.flatMap(({created, sha, commit:{message, url}, files}) => files.map(({filename, status, additions, deletions, patch}) => ({created, sha, message, filename, status, additions, deletions, patch, repo:url.match(/repos[/](?<repo>[\s\S]+)[/]git[/]commits/)?.groups?.repo})))
5558
.filter(({patch}) => (patch ? (patch.match(/\n/mg)?.length ?? 1) : Infinity) < lines)
5659
for (const file of files)
5760
file.language = await imports.language({...file, prefix:login}).catch(() => "unknown")
@@ -60,12 +63,15 @@ export default async function({login, q, imports, data, rest, account}, {enabled
6063
if (snippet) {
6164
//Trim common indent from content and change line feed
6265
if (!snippet.patch.split("\n").shift().endsWith("@@"))
63-
snippet.patch = snippet.patch.replace(/^(?<coord>@@.*?@@)/, "$<coord>\n")
64-
const indent = Math.min(...(snippet.patch.match(/^[+-]? +/mg)?.map(indent => (indent.length ?? Infinity) - indent.startsWith("+") - indent.startsWith("-")) ?? [])) || 0
65-
const content = imports.htmlescape(snippet.patch.replace(/\r\n/mg, "\n").replace(new RegExp(`^([+-]?)${" ".repeat(indent)}`, "mg"), "$1"))
66+
snippet.patch = snippet.patch.replace(/^(?<coord>@@.*?@@).*/, "$<coord>")
67+
const indent = Math.min(...(snippet.patch.match(/^.\s+/mg) ?? [0]).map(line => line.length-1)) || 0
68+
const content = imports.htmlescape(snippet.patch.replace(new RegExp(`^(.)\\s{0,${indent}}`, "mg"), "$1"))
6669

6770
//Format patch
68-
snippet.patch = imports.htmlunescape((await imports.highlight(content, "diff")).trim()).replace(/\n/g, "<br/>")
71+
snippet.patch = imports.htmlunescape(await imports.highlight(content, "diff"))
72+
.trim()
73+
.replace(/\r?\n/g, "<br/>")
74+
.replace(/<span class="token prefix unchanged"> <\/span>/g, '<span class="token prefix unchanged">·</span>')
6975
}
7076

7177
//Results

source/plugins/code/metadata.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ inputs:
3333
min: 100
3434
max: 1000
3535

36+
plugin_activity_days:
37+
description: Events maximum age
38+
type: number
39+
default: 3
40+
min: 0
41+
max: 365
42+
zero: disable
43+
3644
plugin_code_visibility:
3745
description: |
3846
Events visibility
@@ -53,7 +61,6 @@ inputs:
5361
inherits: repositories_skipped
5462

5563
plugin_code_languages:
56-
extras: yes
5764
description: Restrict display to specific languages
5865
type: array
5966
format: comma-separated

source/templates/classic/partials/code.ejs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M2 2.5A2.5 2.5 0 014.5 0h8.75a.75.75 0 01.75.75v12.5a.75.75 0 01-.75.75h-2.5a.75.75 0 110-1.5h1.75v-2h-8a1 1 0 00-.714 1.7.75.75 0 01-1.072 1.05A2.495 2.495 0 012 11.5v-9zm10.5-1V9h-8c-.356 0-.694.074-1 .208V2.5a1 1 0 011-1h8zM5 12.25v3.25a.25.25 0 00.4.2l1.45-1.087a.25.25 0 01.3 0L8.6 15.7a.25.25 0 00.4-.2v-3.25a.25.25 0 00-.25-.25h-3.5a.25.25 0 00-.25.25z"></path></svg>
2222
From <span class="blue space"><%= plugins.code.snippet.repo %></span>
2323
</div>
24+
</section>
25+
<section class="largeable-column-fields">
26+
<div class="field">
27+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M1.5 8a6.5 6.5 0 1113 0 6.5 6.5 0 01-13 0zM8 0a8 8 0 100 16A8 8 0 008 0zm.5 4.75a.75.75 0 00-1.5 0v3.5a.75.75 0 00.471.696l2.5 1a.75.75 0 00.557-1.392L8.5 7.742V4.75z"></path></svg>
28+
On <%= f.date(plugins.code.snippet.created, {time:true, date:true, timeZone:config.timezone?.name}) %>
29+
</div>
30+
</section>
31+
</div>
32+
<div class="row">
33+
<section class="largeable-column-fields">
2434
<div class="field">
2535
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M10.5 7.75a2.5 2.5 0 11-5 0 2.5 2.5 0 015 0zm1.43.75a4.002 4.002 0 01-7.86 0H.75a.75.75 0 110-1.5h3.32a4.001 4.001 0 017.86 0h3.32a.75.75 0 110 1.5h-3.32z"></path></svg>
2636
<%= plugins.code.snippet.message %>

source/templates/classic/style.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,12 +1254,18 @@
12541254
.snippet .body {
12551255
padding-left: 12px;
12561256
}
1257+
.snippet .language-diff {
1258+
font-size: 77%;
1259+
}
12571260
.snippet.additions {
12581261
color: #336543;
12591262
}
12601263
.snippet.deletions {
12611264
color: #9A5256;
12621265
}
1266+
.snippet .token.prefix.unchanged {
1267+
visibility: hidden;
1268+
}
12631269

12641270
/* Markdown and syntax highlighting */
12651271
.markdown b, .markdown i {

0 commit comments

Comments
 (0)