Skip to content

Commit

Permalink
fix: expose language detection
Browse files Browse the repository at this point in the history
  • Loading branch information
bodinsamuel committed Oct 24, 2023
1 parent a0bb7cc commit c7b2266
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
31 changes: 29 additions & 2 deletions src/common/languages.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
type ListItem = {
import path from 'node:path';

export type LangListItem = {
extensions: string[];
group: string | null;
name: string;
type: 'data' | 'markup' | 'programming' | 'prose';
};

// Source: https://github.com/github/linguist/blob/5a0c74277548122267d84283910abd5e0b89380e/lib/linguist/languages.yml#L1528
export const rawList: ListItem[] = [
export const rawList: LangListItem[] = [
{
extensions: ['.bsl', '.os'],
group: null,
Expand Down Expand Up @@ -4428,3 +4430,28 @@ export const rawList: ListItem[] = [

export const languages = rawList.filter((l) => l.type === 'programming');
export const others = rawList.filter((l) => l.type !== 'programming');

/**
* Detect language of a file at this level.
*/
export function detectLang(filename: string): LangListItem | null {
const ext = path.extname(filename);

for (const lang of languages) {
if (!lang.extensions.includes(ext)) {
continue;
}

return lang;
}

for (const lang of others) {
if (!lang.extensions.includes(ext)) {
continue;
}

return lang;
}

return null;
}
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/* eslint-disable import/extensions */
import { analyser } from './analyser/index.js';
import {
LangListItem,
detectLang,
rawList as languageList,
} from './common/languages.js';
import { listIndexed, listTech } from './common/techs.generated.js';
import {
rawList,
Expand Down Expand Up @@ -55,6 +60,7 @@ export {
TechItem,
TechMatcher,
TechType,
LangListItem,
};

export {
Expand Down Expand Up @@ -82,3 +88,8 @@ export const tech = {
list: listTech,
keys: registeredTech,
};

export const lang = {
detect: detectLang,
list: languageList,
};
21 changes: 3 additions & 18 deletions src/payload/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';

import { languages, others } from '../common/languages.js';
import { detectLang } from '../common/languages.js';
import { nid } from '../common/nid.js';
import { rulesComponents, rulesTechs } from '../loader.js';
import type { BaseProvider } from '../provider/base.js';
Expand Down Expand Up @@ -231,24 +231,9 @@ export class Payload implements Analyser {
* Detect language of a file at this level.
*/
detectLang(filename: string) {
const ext = path.extname(filename);

for (const lang of languages) {
if (!lang.extensions.includes(ext)) {
continue;
}

this.addLang(lang.group || lang.name);
return;
}

for (const lang of others) {
if (!lang.extensions.includes(ext)) {
continue;
}

const lang = detectLang(filename);
if (lang) {
this.addLang(lang.group || lang.name);
return;
}
}

Expand Down

0 comments on commit c7b2266

Please sign in to comment.