Skip to content

Commit 2ab5f05

Browse files
authored
Add svg linter and fix incorrect svgs (#30086)
Fixes #30082. Adds a new linter that searches for non-existant SVG images in templates. Output before the fix was: ``` $ make lint-templates SVG "octicon-warning" not found, used in templates/devtest/flex-list.tmpl SVG "octicon-warning" not found, used in templates/devtest/flex-list.tmpl SVG "octicon-markup" not found, used in templates/repo/diff/comment_form.tmpl make: *** [Makefile:438: lint-templates] Error 1 ``` <img width="306" alt="Screenshot 2024-03-25 at 23 31 05" src="https://github.com/go-gitea/gitea/assets/115237/1052d1a9-bfec-4d5a-9cae-f895f78f7c93">
1 parent 274bc00 commit 2ab5f05

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

.github/workflows/files-changed.yml

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ jobs:
7373
- "Makefile"
7474
7575
templates:
76+
- "tools/lint-templates-*.js"
7677
- "templates/**/*.tmpl"
7778
- "pyproject.toml"
7879
- "poetry.lock"

.github/workflows/pull-compliance.yml

+4
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ jobs:
3535
- uses: actions/setup-python@v5
3636
with:
3737
python-version: "3.12"
38+
- uses: actions/setup-node@v4
39+
with:
40+
node-version: 20
3841
- run: pip install poetry
3942
- run: make deps-py
43+
- run: make deps-frontend
4044
- run: make lint-templates
4145

4246
lint-yaml:

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ lint-actions:
434434
$(GO) run $(ACTIONLINT_PACKAGE)
435435

436436
.PHONY: lint-templates
437-
lint-templates: .venv
437+
lint-templates: .venv node_modules
438+
@node tools/lint-templates-svg.js
438439
@poetry run djlint $(shell find templates -type f -iname '*.tmpl')
439440

440441
.PHONY: lint-yaml

templates/devtest/flex-list.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</div>
2626
<div class="flex-item-trailing">
2727
<button class="ui tiny red button">
28-
{{svg "octicon-warning" 14}} CJK文本测试
28+
{{svg "octicon-alert" 14}} CJK文本测试
2929
</button>
3030
<button class="ui tiny primary button">
3131
{{svg "octicon-info" 14}} Button
@@ -54,7 +54,7 @@
5454
</div>
5555
<div class="flex-item-trailing">
5656
<button class="ui tiny red button">
57-
{{svg "octicon-warning" 12}} CJK文本测试 <!-- single CJK text test, it shouldn't be horizontal -->
57+
{{svg "octicon-alert" 12}} CJK文本测试 <!-- single CJK text test, it shouldn't be horizontal -->
5858
</button>
5959
</div>
6060
</div>

templates/repo/diff/comment_form.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
{{end}}
2727

2828
<div class="field footer tw-mx-2">
29-
<span class="markup-info">{{svg "octicon-markup"}} {{ctx.Locale.Tr "repo.diff.comment.markdown_info"}}</span>
29+
<span class="markup-info">{{svg "octicon-markdown"}} {{ctx.Locale.Tr "repo.diff.comment.markdown_info"}}</span>
3030
<div class="tw-text-right">
3131
{{if $.reply}}
3232
<button class="ui submit primary tiny button btn-reply" type="submit">{{ctx.Locale.Tr "repo.diff.comment.reply"}}</button>

tools/lint-templates-svg.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env node
2+
import {readdirSync, readFileSync} from 'node:fs';
3+
import {parse, relative} from 'node:path';
4+
import {fileURLToPath} from 'node:url';
5+
import {exit} from 'node:process';
6+
import fastGlob from 'fast-glob';
7+
8+
const knownSvgs = new Set();
9+
for (const file of readdirSync(new URL('../public/assets/img/svg', import.meta.url))) {
10+
knownSvgs.add(parse(file).name);
11+
}
12+
13+
const rootPath = fileURLToPath(new URL('..', import.meta.url));
14+
let hadErrors = false;
15+
16+
for (const file of fastGlob.sync(fileURLToPath(new URL('../templates/**/*.tmpl', import.meta.url)))) {
17+
const content = readFileSync(file, 'utf8');
18+
for (const [_, name] of content.matchAll(/svg ["'`]([^"'`]+)["'`]/g)) {
19+
if (!knownSvgs.has(name)) {
20+
console.info(`SVG "${name}" not found, used in ${relative(rootPath, file)}`);
21+
hadErrors = true;
22+
}
23+
}
24+
}
25+
26+
exit(hadErrors ? 1 : 0);

0 commit comments

Comments
 (0)