Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(code): add *code directive #23

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions @mizu/code/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Imports
import { fromFileUrl, parse } from "@std/path"
import { Logger } from "@libs/logger"
import { _code } from "./mod.ts"
const logger = new Logger().with({ directive: _code.name })

/** List files from github repository. */
async function tree(repository: string, { branch, path }: { branch: string; path: string }) {
const segments = [...path.split("/"), ""]
let subtree = { url: `https://api.github.com/repos/${repository}/git/trees/${branch}`, tree: [] as Array<{ path: string; type: string }> }
while (segments.length) {
const segment = segments.shift()
logger.debug(`fetching ${subtree.url}`)
const { tree } = await fetch(subtree.url).then((response) => response.json())
subtree = segment ? tree.find(({ path }: { path: string }) => path === segment) : { url: subtree.url, tree }
}
return subtree.tree.filter(({ type }: { type: string }) => type === "blob").map(({ path }: { path: string }) => path)
}

// Extract supported languages
const mapping = {} as Record<PropertyKey, string>
const files = await tree("highlightjs/highlight.js", { branch: "main", path: "src/languages" })
const { default: hljs } = await import(`${_code.import.highlightjs}/lib/core`)
logger.info(`found ${files.length} languages`)
for (const filename of files) {
const language = parse(filename).name
const log = logger.with({ language }).debug("processing")
const { default: syntax } = await import(`${_code.import.highlightjs}/lib/languages/${language}`)
hljs.registerLanguage(language, syntax)
mapping[language] = language
for (const alias of (hljs.getLanguage(language).aliases ?? [])) {
mapping[alias] = language
log.trace("registered alias", alias)
}
}
await Deno.writeTextFile(fromFileUrl(import.meta.resolve("./mapping.json")), JSON.stringify(mapping))
logger.ok("mapping.json generated")
14 changes: 14 additions & 0 deletions @mizu/code/deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@mizu/code",
"version": "0.1.0",
"exports": {
".": "./mod.ts"
},
"tasks": {
// Build the list of mapped languages
"build": "deno run --allow-net=api.github.com --allow-write=mapping.json build.ts"
},
"imports": {
"@npm/highlight.js": "npm:highlight.js@11"
}
}
Loading