Skip to content

Commit 6db5dea

Browse files
author
Raffael Herrmann
committed
feat: make search highlight limit configurable via environment variable
- Add SEARCH_HIGHLIGHT_LIMIT environment variable support - Modify Dockerfile to inject env vars at runtime using envsubst - Create custom nginx.conf for environment variable access - Update docker-compose.yml to use local build with env var - Modify app.js to use configurable limit (-1 for unlimited, positive numbers for limit) - Update index.html to receive injected environment variable - Default value is 3, supports -1 to MAX_SAFE_INTEGER range
1 parent 53106af commit 6db5dea

5 files changed

Lines changed: 58 additions & 7 deletions

File tree

Dockerfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Use the official nginx image as the base image
22
FROM nginx:alpine
33

4+
# Install gettext for envsubst
5+
RUN apk add --no-cache gettext
6+
47
# Accept build argument for version
58
ARG VERSION
69
ENV VERSION=${VERSION}
@@ -14,8 +17,11 @@ RUN ln -s /data /usr/share/nginx/html/data
1417
# Replace version placeholder
1518
RUN sed -i "s/__VERSION__/${VERSION}/g" /usr/share/nginx/html/index.html
1619

20+
# Copy nginx configuration
21+
COPY nginx.conf /etc/nginx/nginx.conf
22+
1723
# Expose port 80
1824
EXPOSE 80
1925

20-
# Start nginx
21-
CMD ["nginx", "-g", "daemon off;"]
26+
# Start nginx with envsubst to inject environment variables
27+
CMD ["/bin/sh", "-c", "envsubst '${SEARCH_HIGHLIGHT_LIMIT}' < /usr/share/nginx/html/index.html > /usr/share/nginx/html/index.html.tmp && mv /usr/share/nginx/html/index.html.tmp /usr/share/nginx/html/index.html && nginx -g 'daemon off;'"]

docker-compose.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
services:
22
masscode-web:
3-
image: ghcr.io/codebude/masscode-web:latest
3+
build:
4+
context: .
45
ports:
56
- "8080:80"
67
volumes:
78
- ./db.json:/data/db.json:ro
9+
environment:
10+
- SEARCH_HIGHLIGHT_LIMIT=${SEARCH_HIGHLIGHT_LIMIT:-3}
811
restart: unless-stopped

nginx.conf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
events {
2+
worker_connections 1024;
3+
}
4+
5+
http {
6+
include /etc/nginx/mime.types;
7+
default_type application/octet-stream;
8+
9+
# Enable environment variable access
10+
env SEARCH_HIGHLIGHT_LIMIT;
11+
12+
server {
13+
listen 80;
14+
server_name localhost;
15+
16+
root /usr/share/nginx/html;
17+
index index.html;
18+
19+
# Enable gzip compression
20+
gzip on;
21+
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
22+
23+
# Handle static files
24+
location / {
25+
try_files $uri $uri/ =404;
26+
}
27+
28+
# Handle data directory
29+
location /data/ {
30+
alias /data/;
31+
try_files $uri $uri/ =404;
32+
}
33+
}
34+
}

public/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ <h1 class="text-2xl font-bold">massCode-Web</h1>
6262
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script>
6363
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
6464
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/keep-markup/prism-keep-markup.min.js"></script>
65+
<script>
66+
// Set global config from environment variables
67+
window.MASSCODE_CONFIG = {
68+
SEARCH_HIGHLIGHT_LIMIT: parseInt('${SEARCH_HIGHLIGHT_LIMIT}') || 3
69+
};
70+
</script>
6571
<script type="module" src="js/app.js?v=__VERSION__"></script>
6672
</body>
6773
</html>

public/js/app.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,18 @@ document.addEventListener('DOMContentLoaded', () => {
311311
function highlightSearchMatches(text, query) {
312312
if (!query.trim()) return text;
313313

314+
const highlightLimit = window.MASSCODE_CONFIG?.SEARCH_HIGHLIGHT_LIMIT ?? 3;
315+
314316
try {
315317
// Try to use the query as a regex pattern
316318
const regex = new RegExp(`(${query})`, 'g'); // case-sensitive, global
317319
let matchCount = 0;
318320
return text.replace(regex, (match, group1) => {
319321
matchCount++;
320-
if (matchCount <= 3) {
322+
if (highlightLimit === -1 || matchCount <= highlightLimit) {
321323
return `<mark class="search-highlight">${group1}</mark>`;
322324
}
323-
return group1; // Return without highlighting for matches beyond 3
325+
return group1; // Return without highlighting for matches beyond limit
324326
});
325327
} catch (error) {
326328
// If regex is invalid, escape special characters and search as literal string
@@ -329,10 +331,10 @@ document.addEventListener('DOMContentLoaded', () => {
329331
let matchCount = 0;
330332
return text.replace(regex, (match, group1) => {
331333
matchCount++;
332-
if (matchCount <= 3) {
334+
if (highlightLimit === -1 || matchCount <= highlightLimit) {
333335
return `<mark class="search-highlight">${group1}</mark>`;
334336
}
335-
return group1; // Return without highlighting for matches beyond 3
337+
return group1; // Return without highlighting for matches beyond limit
336338
});
337339
}
338340
}

0 commit comments

Comments
 (0)