Skip to content

Commit c9d5426

Browse files
committed
Merge #176: chore: update flexsearch to v0.8 (issue #161)
2037252 fix: resolve Svelte 5 warnings in ShareButton (Jose Celano) ec5d972 chore: update flexsearch from v0.7.43 to v0.8.212 (Jose Celano) 295ede9 chore: add pre-commit hook for lint and type checks (Jose Celano) Pull request description: ## Summary Resolves #161 — updates `flexsearch` from **v0.7.43** to **v0.8.212** (breaking change) and fixes all resulting type and Svelte 5 warnings discovered during the process. ## Commits 1. **chore: add pre-commit hook for lint and type checks** - `scripts/pre-commit.sh` — runs Prettier, ESLint, and `svelte-check` before each commit - `.githooks/pre-commit` — git hook entry point delegating to the script - `npm run pre-commit` for manual invocation - `prepare` npm script auto-installs the hook on `npm install` 2. **chore: update flexsearch from v0.7.43 to v0.8.212** - Replace default import with named import: `import { Index } from 'flexsearch'` - Remove `FlexSearch` namespace type: `let postsIndex: Index` - Use `new Index()` instead of `new FlexSearch.Index()` - Cast search results to `number[]` for explicit typing 3. **fix: resolve Svelte 5 warnings in ShareButton** - Wrap `encodedBody`, `encodedSlug`, and `socialLinks` in `$derived()` runes so they stay reactive when props change (was flagged by the new pre-commit hook) ## Testing - `npm run pre-commit` — 0 errors, 0 warnings ✔ - `npm run build` — completes successfully ✔ - `npm run lint` — passes ✔ - `npm run check` — 0 errors, 0 warnings ✔ ACKs for top commit: josecelano: ACK 2037252 Tree-SHA512: 98ffbea457aab05bd24ad4d9bc6fd8c627dc73ae6b66d36e738e868c793bf511c3e796971154c98e226f6c9f600aef5d359f2888b1d7e9e4625925f798faeab0
2 parents 225b77e + 2037252 commit c9d5426

7 files changed

Lines changed: 178 additions & 104 deletions

File tree

.githooks/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
bash "$(git rev-parse --show-toplevel)/scripts/pre-commit.sh"

package-lock.json

Lines changed: 26 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"format": "prettier --write .",
1313
"lint": "prettier --check . && eslint --ignore-pattern .svelte-kit/output .",
1414
"postbuild": "svelte-sitemap --domain https://torrust.com/",
15-
"update-contributors": "tsx scripts/updateContributors.ts"
15+
"update-contributors": "tsx scripts/updateContributors.ts",
16+
"pre-commit": "bash scripts/pre-commit.sh",
17+
"prepare": "git config core.hooksPath .githooks && chmod +x .githooks/pre-commit"
1618
},
1719
"devDependencies": {
1820
"@eslint/compat": "^1.4.1",
@@ -61,7 +63,7 @@
6163
"@tailwindcss/forms": "^0.5.11",
6264
"@tailwindcss/typography": "^0.5.19",
6365
"dateformat": "^5.0.3",
64-
"flexsearch": "^0.7.43",
66+
"flexsearch": "^0.8.212",
6567
"highlight.js": "^11.11.1",
6668
"reading-time": "^1.5.0",
6769
"rehype-pretty-code": "^0.14.1",

scripts/pre-commit.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
# Pre-commit checks: runs lint and type-checking before allowing a commit.
3+
# Install as a git hook automatically via `npm install` (see prepare script).
4+
# Run manually: bash scripts/pre-commit.sh
5+
6+
set -euo pipefail
7+
8+
RED='\033[0;31m'
9+
GREEN='\033[0;32m'
10+
YELLOW='\033[1;33m'
11+
NC='\033[0m' # No Color
12+
13+
pass() { echo -e "${GREEN}${NC} $1"; }
14+
fail() { echo -e "${RED}${NC} $1"; }
15+
info() { echo -e "${YELLOW}${NC} $1"; }
16+
17+
echo ""
18+
info "Running pre-commit checks..."
19+
echo ""
20+
21+
FAILED=0
22+
23+
# 1. Prettier formatting check
24+
info "Checking code formatting (Prettier)..."
25+
if npx prettier --check . --log-level warn; then
26+
pass "Formatting OK"
27+
else
28+
fail "Formatting issues found. Run: npm run format"
29+
FAILED=1
30+
fi
31+
echo ""
32+
33+
# 2. ESLint
34+
info "Running ESLint..."
35+
if npx eslint --ignore-pattern .svelte-kit/output .; then
36+
pass "ESLint OK"
37+
else
38+
fail "ESLint errors found."
39+
FAILED=1
40+
fi
41+
echo ""
42+
43+
# 3. TypeScript + Svelte type checking
44+
info "Running TypeScript / Svelte checks..."
45+
if npm run check --silent; then
46+
pass "Type checks OK"
47+
else
48+
fail "Type errors found. Fix them before committing."
49+
FAILED=1
50+
fi
51+
echo ""
52+
53+
# Summary
54+
if [ "$FAILED" -eq 0 ]; then
55+
echo -e "${GREEN}All pre-commit checks passed.${NC}"
56+
echo ""
57+
exit 0
58+
else
59+
echo -e "${RED}One or more pre-commit checks failed. Commit aborted.${NC}"
60+
echo ""
61+
exit 1
62+
fi

src/lib/components/singletons/ShareButton.svelte

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
let showDropdown: boolean = $state(false);
1414
1515
const encodedSubject = encodeURIComponent('I wanted you to see this blog post');
16-
const encodedBody = encodeURIComponent(
17-
`${props.title} is a really interesting blog post from Torrust. Check it out here: ${siteBaseUrl}/${props.slug}`
16+
const encodedBody = $derived(
17+
encodeURIComponent(
18+
`${props.title} is a really interesting blog post from Torrust. Check it out here: ${siteBaseUrl}/${props.slug}`
19+
)
1820
);
19-
const encodedSlug = encodeURIComponent(props.slug);
21+
const encodedSlug = $derived(encodeURIComponent(props.slug));
2022
2123
const unescapedHref = (href: string) => {
2224
return href;
2325
};
2426
25-
const socialLinks = [
27+
const socialLinks = $derived([
2628
{
2729
text: 'Share via email',
2830
href: `mailto:?subject=${encodedSubject}&body=${encodedBody}`
@@ -39,7 +41,7 @@
3941
text: 'Share on X',
4042
href: `https://twitter.com/share?url=${siteBaseUrl}/${encodedSlug}&text=${props.title}`
4143
}
42-
];
44+
]);
4345
4446
function toggleDropdown() {
4547
showDropdown = !showDropdown;

src/lib/utils/search.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import FlexSearch from 'flexsearch';
1+
import { Index } from 'flexsearch';
22
import type { BlogPost } from '$lib/utils/types';
33

44
interface Post {
@@ -8,11 +8,11 @@ interface Post {
88
tags: string[];
99
}
1010

11-
let postsIndex: FlexSearch.Index;
11+
let postsIndex: Index;
1212
let posts: Post[];
1313

1414
export function createPostsIndex(data: BlogPost[]) {
15-
postsIndex = new FlexSearch.Index({ tokenize: 'forward' });
15+
postsIndex = new Index({ tokenize: 'forward' });
1616

1717
const mappedPosts = data.map((post) => ({
1818
slug: post.slug,
@@ -35,10 +35,10 @@ export function searchPostsIndex(searchTerm: string) {
3535
}
3636

3737
const match = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
38-
const results = postsIndex.search(match);
38+
const results = postsIndex.search(match) as number[];
3939

4040
return results
41-
.map((index) => posts[index as number])
41+
.map((index: number) => posts[index])
4242
.map(({ slug, title = '', excerpt = '', tags = [] }) => {
4343
return {
4444
slug,

0 commit comments

Comments
 (0)