Skip to content

Commit

Permalink
feat(web): moved the Node.js example to its own page
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-potter committed Jan 2, 2025
1 parent d804e46 commit 2075d2c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 36 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/harper.js/examples/commonjs-simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node index.cjs"
"start": "node index.js"
},
"dependencies": {
"harper.js": "^0.13.0"
Expand Down
36 changes: 1 addition & 35 deletions packages/web/src/routes/docs/harperjs/introduction/+page.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,6 @@ Today, it serves as the foundation for our [Obsidian plugin](/docs/integrations/
`harper.js` is an ECMAScript module designed to be easy to import into any project.
On the inside, it uses a copy of Harper's core algorithm compiled to [WebAssembly](https://webassembly.org/).

It can be imported [natively in a browser](./CDN) or through [npm](https://www.npmjs.com/package/harper.js).
It can be imported [natively in a browser](./CDN) or through [npm](https://www.npmjs.com/package/harper.js) and [consumed in Node.js](./node).

@install-pkg(harper.js)

## Sample Usage:

```js
let harper = require('harper.js');

async function main() {
// We cannot use `WorkerLinter` on Node.js since it relies on web-specific APIs.
let linter = new harper.LocalLinter();
linter.setup();

let lints = await linter.lint('This is a example of how to use `harper.js`.');

console.log('Here are the results of linting the above text:');

for (let lint of lints) {
console.log(' - ', lint.span().start, ':', lint.span().end, lint.message());

if (lint.suggestion_count() != 0) {
console.log('Suggestions:');

for (let sug of lint.suggestions()) {
console.log(
'\t - ',
sug.kind() == 1 ? 'Remove' : 'Replace with',
sug.get_replacement_text()
);
}
}
}
}

main();
```
15 changes: 15 additions & 0 deletions packages/web/src/routes/docs/harperjs/node/+page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Using Harper in Node.js
---

Harper.js can run in Node.js.
There is just one consideration: as described in [more detailed here](./linting), we cannot use the `WorkerLinter`.
That means we must use the `LocalLinter`.

Additionally, since `harper.js` is an ECMAScript module, it must be imported in a relatively recent version of Node.js.

## Example Code

The example below can be found in [the Harper monorepo.](https://github.com/Automattic/harper/tree/master/packages/harper.js/examples/commonjs-simple)

@code(../../../../../../harper.js/examples/commonjs-simple/index.js)
4 changes: 4 additions & 0 deletions packages/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export default defineConfig({
title: 'Linting',
to: '/docs/harperjs/linting'
},
{
title: 'Node.js',
to: '/docs/harperjs/node'
},
{
title: 'CDN',
to: '/docs/harperjs/CDN'
Expand Down

0 comments on commit 2075d2c

Please sign in to comment.