39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import { basename } from "node:path";
|
|
|
|
import type { ParsedTrackRef } from "../types.js";
|
|
import { dedupeTrackRefs, parseArtistTitle } from "./importer-utils.js";
|
|
|
|
function parseExtInf(line: string): string | undefined {
|
|
const comma = line.indexOf(",");
|
|
if (comma === -1 || comma === line.length - 1) {
|
|
return undefined;
|
|
}
|
|
return line.slice(comma + 1).trim();
|
|
}
|
|
|
|
export function parseM3u(content: string): ParsedTrackRef[] {
|
|
const refs: ParsedTrackRef[] = [];
|
|
let pendingExtInf: string | undefined;
|
|
for (const rawLine of content.split(/\r?\n/u)) {
|
|
const line = rawLine.trim();
|
|
if (!line) {
|
|
continue;
|
|
}
|
|
if (line.startsWith("#EXTINF:")) {
|
|
pendingExtInf = parseExtInf(line);
|
|
continue;
|
|
}
|
|
if (line.startsWith("#")) {
|
|
continue;
|
|
}
|
|
refs.push(...parseArtistTitle(pendingExtInf ?? basename(line)));
|
|
pendingExtInf = undefined;
|
|
}
|
|
return dedupeTrackRefs(refs);
|
|
}
|
|
|
|
export async function readM3u(path: string): Promise<ParsedTrackRef[]> {
|
|
return parseM3u(await readFile(path, "utf8"));
|
|
}
|