-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathprompt.ts
More file actions
50 lines (44 loc) · 2.38 KB
/
prompt.ts
File metadata and controls
50 lines (44 loc) · 2.38 KB
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
// src/content-render/liquid/prompt.ts
// Defines {% prompt %}…{% endprompt %} to wrap its content in <code> and append the Copilot icon.
import octicons from '@primer/octicons'
import { generatePromptId } from '../lib/prompt-id'
interface LiquidTag {
type: 'block'
templates?: any[] // Note: Using 'any' because liquidjs doesn't provide proper types for template objects
// Note: Using 'any' for liquid-related parameters because liquidjs doesn't provide comprehensive TypeScript definitions
parse(tagToken: any, remainTokens: any): void
render(scope: any): Generator<any, string, unknown>
}
export const Prompt: LiquidTag = {
type: 'block',
// Collect everything until {% endprompt %}
parse(tagToken: any, remainTokens: any): void {
this.templates = []
const stream = this.liquid.parser.parseStream(remainTokens)
stream
.on('template', (tpl: any) => this.templates.push(tpl))
.on('tag:endprompt', () => stream.stop())
.on('end', () => {
throw new Error(`{% prompt %} tag not closed`)
})
stream.start()
},
// Render the inner Markdown, wrap in <code>, then append the SVG
*render(scope: any): Generator<any, string, unknown> {
const content = yield this.liquid.renderer.renderTemplates(this.templates, scope)
const contentString = String(content)
// build a URL with the prompt text encoded as query parameter
const promptParam: string = encodeURIComponent(contentString)
const href: string = `https://github.com/copilot?prompt=${promptParam}`
// Use murmur hash for deterministic ID (avoids hydration mismatch)
const promptId: string = generatePromptId(contentString)
// Show long text on larger screens and short text on smaller screens (set via accessibility.scss)
const promptLabelLong: string = 'Run this prompt in Copilot Chat'
const promptLabelShort: string = 'Run prompt'
return [
`<code id="${promptId}">${content}</code>`,
`<a href="${href}" target="_blank" class="tooltipped tooltipped-n ml-1 copilot-prompt-long" aria-label="${promptLabelLong}" aria-describedby="${promptId}" style="text-decoration:none;">${octicons.copilot.toSVG()}</a>`,
`<a href="${href}" target="_blank" class="tooltipped tooltipped-n ml-1 copilot-prompt-short" aria-label="${promptLabelShort}" aria-describedby="${promptId}" style="text-decoration:none;">${octicons.copilot.toSVG()}</a>`,
].join('')
},
}