Skip to content

Commit 3dab1eb

Browse files
authored
Merge branch 'main' into fix-cassandra
2 parents 178a7d2 + 214007e commit 3dab1eb

13 files changed

Lines changed: 69 additions & 11 deletions

File tree

.djlint_rules.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- rule:
2+
name: C001
3+
message: Script tags with src= must include a nonce= attribute
4+
python_module: djlint_custom_rules

.github/workflows/pr.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
- name: Install python dependencies
8787
run: |
8888
python3 -m pip install --upgrade pip
89-
sudo pip3 install djlint
89+
sudo pip3 install "$(grep -m1 -E '^djlint==' requirements.txt)"
9090
9191
- name: Get changed HTML files in the templates folder
9292
id: changed-files
@@ -99,7 +99,7 @@ jobs:
9999
if: env.CHANGED_FILES != ''
100100
run: |
101101
echo "The following files have changed: $CHANGED_FILES"
102-
djlint $CHANGED_FILES --lint --profile="jinja"
102+
PYTHONPATH=. djlint $CHANGED_FILES --lint --profile="jinja"
103103
104104
test-python:
105105
runs-on: ubuntu-22.04

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ yarn lint-scss # stylelint
8888
yarn format-python # black --line-length 79
8989
yarn format-prettier # prettier
9090

91-
djlint templates/path/to/file.html --lint --profile=jinja # djlint for html/jinja - uses `.djlintrc`
91+
PYTHONPATH=. djlint templates/path/to/file.html --lint --profile=jinja # djlint for html/jinja - uses `.djlintrc` + custom rules in `.djlint_rules.yaml`
9292
```
9393

9494
## External Service Dependencies

build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ let entries = {
2020
"watch-consent-changes": "./static/js/watch-consent-changes.js",
2121
"events": "./static/js/events.js",
2222
"in-page-navigation": "./static/js/in-page-navigation.js",
23+
"prism": "./static/js/prism.js",
2324
};
2425

2526
const isDev = process && process.env && process.env.NODE_ENV === "development";

djlint_custom_rules.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Custom djlint linter rules for canonical.com.
2+
3+
Loaded by djlint via ``.djlint_rules.yaml`` (``python_module`` rules). Unlike
4+
djlint pattern rules, ``python_module`` rules receive the full HTML and are not
5+
filtered by djlint's ignored-block logic, so they can inspect ``<script>`` tags
6+
(which djlint otherwise treats as ignored blocks).
7+
"""
8+
9+
from __future__ import annotations
10+
11+
import re
12+
13+
from djlint.lint import get_line
14+
15+
# Match a single <script ...> opening tag.
16+
SCRIPT_OPEN_TAG = re.compile(r"<script\b[^>]*>", re.IGNORECASE)
17+
HAS_SRC = re.compile(r"\bsrc=", re.IGNORECASE)
18+
HAS_NONCE = re.compile(r"\bnonce\b", re.IGNORECASE)
19+
20+
21+
def run(rule, config, html, filepath, line_ends, *args, **kwargs):
22+
"""C001: <script src=...> tags must include a nonce= attribute."""
23+
errors = []
24+
for match in SCRIPT_OPEN_TAG.finditer(html):
25+
tag = match.group()
26+
if not HAS_SRC.search(tag):
27+
continue
28+
if HAS_NONCE.search(tag):
29+
continue
30+
snippet = re.sub(r"\s+", " ", tag)[:30]
31+
errors.append({
32+
"code": rule["name"],
33+
"line": get_line(match.start(), line_ends),
34+
"match": snippet,
35+
"message": rule["message"],
36+
})
37+
return errors

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
"format-prettier": "prettier -w 'static/js/*.{js,jsx,ts,tsx}' 'static/sass/*.scss'",
1212
"lint-python": "flake8 --extend-ignore=E203 webapp tests && black --check --line-length 79 webapp tests",
1313
"lint-scss": "stylelint static/**/*.scss",
14+
"lint-html": "PYTHONPATH=. djlint templates/ --lint",
1415
"serve": "./entrypoint 0.0.0.0:${PORT}",
1516
"start": "yarn run build && concurrently --raw 'yarn run watch' 'yarn run serve'",
16-
"test": "yarn run lint-scss && yarn run lint-python && yarn run test-python",
17+
"test": "yarn run lint-scss && yarn run lint-python && yarn run lint-html && yarn run test-python",
1718
"test-python": "python3 -m unittest discover tests",
1819
"test-js": "jest --env=jsdom",
1920
"test-marketo": "python3 -m unittest tests.test_marketo",
@@ -44,6 +45,7 @@
4445
"lottie-web": "^5.13.0",
4546
"postcss": "8.5.10",
4647
"postcss-cli": "10.1.0",
48+
"prismjs": "1.30.0",
4749
"react": "^18.2.0",
4850
"react-dom": "^18.2.0",
4951
"react-router-dom": "^6.24.1",

scripts/build-modules.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ cp node_modules/venobox/dist/venobox.min.js static/js/modules/venobox/venobox.mi
3131
mkdir -p static/js/modules/vanilla-framework/js
3232
cp -r node_modules/vanilla-framework/templates/_macros/ static/js/modules/vanilla-framework
3333
cp -r node_modules/vanilla-framework/templates/static/js/tabs.js static/js/modules/vanilla-framework/js/.
34+
35+
mkdir -p static/js/dist/prism-components && cp -r node_modules/prismjs/components/*.min.js static/js/dist/prism-components/

static/js/prism.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Prism from "prismjs";
2+
import "prismjs/themes/prism.css";
3+
import "prismjs/plugins/autoloader/prism-autoloader";
4+
import "prismjs/plugins/keep-markup/prism-keep-markup";
5+
6+
Prism.plugins.autoloader.languages_path = "/static/js/dist/prism-components/";
7+
8+
export default Prism;

templates/base_index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
Must run before the deferred lite-youtube module upgrades the
7171
element.
7272
-->
73-
<script nonce="{{ csp_nonce }}">window.liteYouTubeNonce = "{{ csp_nonce }}";</script>
73+
<script>window.liteYouTubeNonce = "{{ csp_nonce }}";</script>
7474

7575
{% block extra_metatags %}{% endblock %}
7676

templates/blog/article.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
}
4848
</script>
4949
{# djlint: on #}
50+
<script src="{{ versioned_static('js/dist/prism.js') }}" nonce="{{ csp_nonce }}" defer></script>
5051
{% endblock %}
5152

5253
{% block content %}
@@ -62,13 +63,11 @@
6263
{% if article.author and article.author.id == 217 %}
6364
<img src="https://assets.ubuntu.com/v1/f16c40d0-Favicon+-+CoF.svg"
6465
class="p-media-object__image u-align-self-center"
65-
alt=""
66-
/>
66+
alt="" />
6767
{% else %}
6868
<img src="{{ article.author.avatar_urls['96'] }}"
6969
class="p-media-object__image is-round u-align-self-center"
70-
alt=""
71-
/>
70+
alt="" />
7271
{% endif %}
7372
{% endif %}
7473

0 commit comments

Comments
 (0)