Skip to content

Commit

Permalink
Fix build loop and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Feb 25, 2023
1 parent b00ebfa commit 9bc7db2
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ coverage
public
.env
dependency-graph.html
test-out.js
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# @breadcrum/bookmarklet
[![Actions Status](https://github.com/hifiwi-fi/bc-bookmarklet/workflows/tests/badge.svg)](https://github.com/hifiwi-fi/bc-bookmarklet/actions)
[![latest version](https://img.shields.io/npm/v/@breadcrum/bookmarklet.svg)](https://www.npmjs.com/package/@breadcrum/bookmarklet)
[![Coverage Status](https://coveralls.io/repos/github/hifiwi-fi/bc-bookmarklet/badge.svg?branch=master)](https://coveralls.io/github/hifiwi-fi/bc-bookmarklet?branch=master)
[![downloads](https://img.shields.io/npm/dm/@breadcrum/bookmarklet.svg)](https://npmtrends.com/@breadcrum/bookmarklet)

WIP - nothing to see here

This is the bookmarklet for Breadcrum.net.

```
npm install bookmarklet
Expand All @@ -10,9 +14,27 @@ npm install bookmarklet
## Usage

``` js
import { bookmarklet } from '@breadcrum/bookmarklet'
import bookmarkletText from '@breadcrum/bookmarklet'

const bookmarkletText = getBookmarklet({
TARGET_URL: 'breadcrum.net',
WINDOW_TITLE: 'Breadcrum'
})
```

Returns the bookmarklet text with simple runtime customizations so that environments can fix hard-coded variables.

## How this works

`esbuild` used used to build standard, modular esm code into a minified `iffi` target, and then the reults are stored in a json document that gets customised with `.replaceAll` in the exported `getBookmarklet` function.

## See also

This bookmarklet was based off of the following resources:

- [joelcarranza/particular-pinboard](https://github.com/joelcarranza/particular-pinboard)


## License

MIT
4 changes: 2 additions & 2 deletions dist/out.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/out.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const tagKeywords = {

const urlTags = {
'gist.github.com/([^/]+)/([^/]+)': 'repo',
'github.com\/([^\/]+)\/([^\/]+)': 'repo',
'github.com\/([^\/]+)\/([^\/]+)': 'repo', // eslint-disable-line no-useless-escape
'github.com/([^/]+)$': 'person',
'twitter.com/([^/]+)/status/.*$': 'comment',
'twitter.com/([^/]+)$': 'person',
Expand All @@ -45,7 +45,7 @@ const urlTags = {
'imdb.com/name': 'person',
'news.ycombinator.com/item': 'comment',
'reddit.com': 'comment',
'blog\.': 'blog',
'blog\.': 'blog', // eslint-disable-line no-useless-escape
'schema.org/\\S+': 'type',
'goodreads.com/book/show/': 'book'
}
Expand All @@ -60,6 +60,9 @@ const descriptionTweaks = {
'www.kickstarter.com': '.short-blurb'
}

const height = 680
const width = 710

// limit long titles and descriptions, mostly to avoid 'HTTP/1.0 414 Request URI too long'
const textLengthLimit = 1000

Expand Down Expand Up @@ -131,7 +134,7 @@ const getTitle = function () {

// method 1 - look for link to self with text that is contained in title

var a_text = selectFromNodeList(document.getElementsByTagName('A'), function (a) {
let a_text = selectFromNodeList(document.getElementsByTagName('A'), function (a) {
if (a.href === url) {
a_text = elementText(a)
if (isSubtitle(a_text)) {
Expand Down Expand Up @@ -287,7 +290,8 @@ const processAdditionalArgs = function (text) {
let re, matches
for (const url in additionalEntities) {
re = url instanceof RegExp ? url : new RegExp('\\b' + url + '\\b', 'i')
if (matches = text.match(re)) {
if (text.match(re)) {
matches = text.match(re)
return additionalEntities[url](matches)
}
}
Expand All @@ -311,7 +315,7 @@ if (appUrl) {
])
window.location = appUrl + args.join('')
} else {
const pin = open('http://TARGET_URL/add?' + args.join(''), 'WINDOW_TITLE', 'toolbar=no,width=610,height=350')
const pin = open('http://TARGET_URL/bookmarks/add?' + args.join(''), 'WINDOW_TITLE', `toolbar=no,width=${width},height=${height}`)

// nice to have: load json file of recent type->names maps to avoid dupes
if (additional) {
Expand All @@ -322,7 +326,7 @@ if (appUrl) {
'&tags=', encodeURIComponent(additional.tags.join(' '))
]

open('http://TARGET_URL/add?' + additionalArgs.join(''), 'WINDOW_TITLE 2', 'toolbar=no,width=610,height=350')
open('http://TARGET_URL/bookmarks/add?' + additionalArgs.join(''), 'WINDOW_TITLE 2', `toolbar=no,width=${width},height=${height}`)
}

// Send the window to the background if readlater mode.
Expand Down
9 changes: 7 additions & 2 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import tap from 'tap'
import { hello } from './index.js'
import getBookmarklet from './dist/out.js'

tap.test('index export', async (t) => {
t.equal(hello(), 'world')
const bookmarkletText = getBookmarklet({
TARGET_URL: 'breadcrum.net',
WINDOW_TITLE: 'Breadcrum'
})

t.ok(bookmarkletText, 'Got the bookmarklet text')
})
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "@breadcrum/bookmarklet",
"description": "WIP - nothing to see here",
"description": "Bookmarklet for breadcrum.net",
"files": [
"*.js",
"lib/*.js"
"dist/*.js",
"dist/*.js.map"
],
"version": "0.0.0",
"author": "Bret Comnes <[email protected]> (https://bret.io)",
Expand Down Expand Up @@ -32,22 +32,24 @@
"type": "module",
"module": "index.js",
"exports": {
"import": "./index.js"
"import": "./dist/out.js"
},
"repository": {
"type": "git",
"url": "https://github.com/hifiwi-fi/bc-bookmarklet.git"
},
"scripts": {
"prepare": "rm -rf dist && run-s build",
"prepublishOnly": "git push --follow-tags && gh-releas -y",
"test": "run-s test:*",
"test:deptree": "depcruise --validate .dependency-cruiser.json .",
"test:standard": "standard --verbose | snazzy",
"test:tap": "METRICS=0 c8 tap --no-coverage --color",
"version": "run-s prepare version:*",
"version:changelog": "auto-changelog -p --template keepachangelog auto-changelog --breaking-pattern 'BREAKING CHANGE:'",
"version:git": "git add CHANGELOG.md",
"deps": "depcruise --exclude '^node_modules' --output-type dot . | dot -T svg | depcruise-wrap-stream-in-html > dependency-graph.html"
"version:git": "git add CHANGELOG.md dist",
"deps": "depcruise --exclude '^node_modules' --output-type dot . | dot -T svg | depcruise-wrap-stream-in-html > dependency-graph.html",
"build": "node scripts/build.js"
},
"standard": {
"ignore": [
Expand Down
19 changes: 19 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { dirname } from 'desm'
import { join } from 'path'
import { writeFile } from 'fs/promises'
import { makeBookmarklet } from './make-bookmarklet.js'

const __dirname = dirname(import.meta.url)

const inFile = join(__dirname, '../index.js')
const outFile = join(__dirname, '../dist/out.js')

const { dest } = await makeBookmarklet(inFile, outFile)

const created = await import(dest)

const bookmarkletFn = created.default({ TARGET_URL: 'breadcrum.net', WINDOW_TITLE: 'breadcrum' })

await writeFile(join(__dirname, '../test-out.js'), bookmarkletFn)

console.log('done')
22 changes: 7 additions & 15 deletions build.js → scripts/make-bookmarklet.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import esbuild from 'esbuild'
import { dirname } from 'desm'
import { join } from 'path'
import { writeFile } from 'fs/promises'

const __dirname = dirname(import.meta.url)
const wrapperPath = join(__dirname, 'wrapper.js')

async function makeBookmarklet (src, dest) {
export async function makeBookmarklet (src, dest) {
const buildOpts = {
entryPoints: [src],
// logLevel: 'silent',
Expand All @@ -23,12 +22,12 @@ async function makeBookmarklet (src, dest) {
}

const result = await esbuild.build(buildOpts)

console.dir({ result }, { colors: true, depth: 999 })
const fileData = result.outputFiles[0]

const innerText = fileData.text

const bookmarklet = `javascript:(${innerText.trim()})`
const bookmarklet = `javascript:(${innerText.trim().slice(0, -1)})`

const buildWrapperOpts = {
entryPoints: [wrapperPath],
Expand All @@ -49,15 +48,8 @@ async function makeBookmarklet (src, dest) {

const wrapperResult = await esbuild.build(buildWrapperOpts)
console.dir({ wrapperResult }, { colors: true, depth: 999 })
return {
src,
dest
}
}

const inFile = join(__dirname, 'index.js')
const outFile = join(__dirname, 'dist/out.js')

await makeBookmarklet(inFile, outFile)

const created = await import(outFile)

const bookmarkletFn = created.default({ TARGET_URL: 'breadcrum.net', WINDOW_TITLE: 'breadcrum' })

await writeFile('test-out.js', bookmarkletFn)
File renamed without changes.
Loading

0 comments on commit 9bc7db2

Please sign in to comment.