Readme

moduleHighlightValueLink: Link to a Val With a Value or Method's Code Highlighted

Examples:

Create valimport { moduleHighlightValueLink, getRedirectUrl } from "https://esm.town/v/postpostscript/moduleHighlightValueLink"; console.log(await moduleHighlightValueLink("@std/email", "email")) // https://val.town/v/std/email#L6-42 console.log(await moduleHighlightValueLink("@postpostscript/moduleHighlightValueLink", "moduleHighlightValueLink")) // https://val.town/v/postpostscript/moduleHighlightValueLink#L6-20 // get URL you can imbed in an iframe console.log(getRedirectUrl("@postpostscript/moduleHighlightValueLink", "getRedirectUrl", true)); // https://postpostscript-modulehighlightvaluelink.web.val.run/?embed=1&module=%40postpostscript%2FmoduleHighlightValueLink&name=getRedirectUrl

Iframe example:

Create valimport { htmlResponse } from "https://esm.town/v/postpostscript/html"; import { getRedirectUrl } from "https://esm.town/v/postpostscript/moduleHighlightValueLink"; export default async function(req: Request): Promise<Response> { return htmlResponse` <iframe src="${getRedirectUrl(import.meta.url, "default", true)}" width="100%" height="100%"> </iframe> `; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import * as queryString from "https://deno.land/x/querystring@v1.0.2/mod.js";
import { parse, print } from "https://deno.land/x/swc@0.2.1/mod.ts";
import { findASTValueDefinition, findSpanLocation, getSourceAST } from "https://esm.town/v/postpostscript/ast";
import {
getValEndpointFromUrl,
getValNameFromUrl,
getValUrlFromName,
moduleSource,
} from "https://esm.town/v/postpostscript/meta";
export async function moduleHighlightValueLink(module: string, name: string, log = false) {
const { source, node: ast } = await getSourceAST(await moduleSource(module));
const node = findASTValueDefinition(ast, name);
const range = node && findSpanLocation(source, ast, node.span);
const locationText = range && (
range.start === range.end
? `L${range.start}`
: `L${range.start}-${range.end}`
);
if (log) {
console.log({
module,
name,
astSpan: ast.span,
node,
range,
locationText,
});
}
const url = module.charAt(0) === "@"
? getValUrlFromName(module, "val.town")
: getValUrlFromName(getValNameFromUrl(module), "val.town");
return url + (locationText ? `#${locationText}` : "");
}
export function getRedirectUrl(module: string, name: string, embed = false) {
const qs = new URLSearchParams();
if (embed) {
qs.set("embed", "1");
}
qs.set("module", module);
qs.set("name", name);
return getValEndpointFromUrl(import.meta.url) + "/?" + qs.toString();
}
export default async function(req: Request) {
const qs = queryString.parse(req.url.split("?")[1] || "");
const module = qs.module?.trim();
const name = qs.name?.trim();
const embed = qs.embed?.trim();
if (!(module && name)) {
return Response.json({
error: "qs params `module` and `name` must be set",
});
}
const url = await moduleHighlightValueLink(module, name, true);
return Response.redirect(
embed
? url.replace("val.town/v/", "val.town/embed/")
: url,
);
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.