0.2.0 • Published 2 years ago
goggledy v0.2.0
Goggledy is a TypeScript library that lets you easily interact with Brave Search Goggles.
It can be used to parse Goggle code into a JavaScript representation, or to generate goggle code from a Goggle object.
This opens the door for applications that want to interact with Goggles in a more programmatic way. E.g. interactive editors, code generators, etc.
Parsing Goggles
Parsing a goggle into a structured JavaScript representation holding all
the information about the Goggle with Goggle.parse()
:
import { Goggle } from 'goggledy'
const goggle = Goggle.parse(
`! name: Some name
! description: Some description`,
)
console.log(goggle.metaData.name) // Some name
Generating Goggles
Generating Goggles from JavaScript representation with Goggle.toString()
:
import * as goggledy from 'goggledy'
const goggle = new goggledy.Goggle(
// Goggle lines, e.g. instructions, comments, etc.
[
new goggledy.GoggleEmpty(),
new goggledy.GoggleComment('Some comment'),
new goggledy.GoggleInstruction('pattern', {
site: 'example.org',
discard: true,
}),
],
// Goggle meta data
{
name: 'Some name',
description: 'Some description',
// This is a shorthand for:
// `goggledy.GoggleMeta('name', 'Some name')`
// under the lines array above.
},
)
console.log(goggle.toString())
// ! name: Some name
// ...
Comparison
See how to construct a Goggle in Goggledy and what values it returns with its
toString()
method, and when it is passed to JSON.stringify()
.
! name: Some name
! description: Some description
! public: true
new Goggle([], {
name: 'Some name',
description: 'Some description',
public: true,
})
{
"metaData": {
"name": "Some name",
"description": "Some description",
"public": true
},
"lines": [
{
"type": "meta",
"key": "name",
"value": "Some name"
},
{
"type": "meta",
"key": "description",
"value": "Some description"
},
{
"type": "meta",
"key": "public",
"value": true
}
]
}
pattern$boost=10,incontent,site=example.org
new GoggleInstruction('pattern', {
boost: 10,
incontent: true,
site: 'example.org',
})
{
"type": "instruction",
"pattern": "pattern",
"options": {
"boost": 10,
"incontent": true,
"site": "example.org"
}
}
! name: value
new GoggleMeta('name', 'value')
{
"type": "meta",
"key": "name",
"value": "value"
}
! comment
new GoggleComment(' comment')
{
"type": "comment",
"value": " comment"
}
new GoggleEmpty()
{
"type": "empty"
}
Who uses Goggledy?
- Gearchy uses Goggledy for two matters, parsing Goggles stored in GitHub gists to populate its interactive Goggle editor, and generating Goggles from the editor to store them back in GitHub gists.