Make language selector also search through the languages' filename suffixes (using the guesslang attribute)

This makes the "cpp" string match C++, and "cs" match C#.
This commit is contained in:
Jonatan Heyman 2025-01-03 15:43:17 +01:00
parent ff88091e28
commit c87ea672b0
2 changed files with 21 additions and 3 deletions

View File

@ -5,7 +5,8 @@
const items = LANGUAGES.map(l => { const items = LANGUAGES.map(l => {
return { return {
"token": l.token, "token": l.token,
"name": l.name "name": l.name,
"guesslang": l.guesslang,
} }
}).sort((a, b) => { }).sort((a, b) => {
return a.name.localeCompare(b.name) return a.name.localeCompare(b.name)
@ -35,12 +36,13 @@
return items return items
} }
const searchResults = fuzzysort.go(this.filter, items, { const searchResults = fuzzysort.go(this.filter, items, {
keys: ['name'], keys: ['name', 'guesslang'],
}) })
return searchResults.map(result => { return searchResults.map(result => {
const highlight = result[0].highlight("<b>", "</b>")
return { return {
"token": result.obj.token, "token": result.obj.token,
"name": result[0].highlight("<b>", "</b>") "name": highlight || result.obj.name,
} }
}) })
}, },

View File

@ -0,0 +1,16 @@
import { test, expect } from "@playwright/test";
import { HeynotePage } from "./test-utils.js";
let heynotePage
test.beforeEach(async ({ page }) => {
heynotePage = new HeynotePage(page)
await heynotePage.goto()
})
test("test language selector search by file ending", async ({ page }) => {
await page.locator("body").press(heynotePage.agnosticKey("Mod+L"))
await page.locator("body").pressSequentially("cpp")
await expect(page.locator("css=.language-selector .items > li.selected")).toHaveText("C++")
})