From bac07a8a9db0a229599740b2f90f79e53ccb8ce0 Mon Sep 17 00:00:00 2001 From: AminWhat <88392440+aminwhat@users.noreply.github.com> Date: Thu, 2 Jan 2025 12:36:02 +0330 Subject: [PATCH 1/7] Update +page.md please add the sample, it took me a while to find the sample code --- .../docs/harperjs/introduction/+page.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/web/src/routes/docs/harperjs/introduction/+page.md b/packages/web/src/routes/docs/harperjs/introduction/+page.md index 1ec08294..c128f95d 100644 --- a/packages/web/src/routes/docs/harperjs/introduction/+page.md +++ b/packages/web/src/routes/docs/harperjs/introduction/+page.md @@ -21,3 +21,36 @@ On the inside, it uses a copy of Harper's core algorithm compiled to [WebAssembl It can be imported [natively in a browser](./CDN) or through [npm](https://www.npmjs.com/package/harper.js). @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(); + + 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(); +``` From 5c23c5b9ba515a907e2302d8d619ed50518fd8e8 Mon Sep 17 00:00:00 2001 From: aminwhat Date: Thu, 2 Jan 2025 12:47:06 +0330 Subject: [PATCH 2/7] fix spacing --- .../docs/harperjs/introduction/+page.md | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/web/src/routes/docs/harperjs/introduction/+page.md b/packages/web/src/routes/docs/harperjs/introduction/+page.md index c128f95d..447c14fe 100644 --- a/packages/web/src/routes/docs/harperjs/introduction/+page.md +++ b/packages/web/src/routes/docs/harperjs/introduction/+page.md @@ -25,31 +25,31 @@ It can be imported [natively in a browser](./CDN) or through [npm](https://www.n ## Sample Usage: ```js -let harper = require('harper.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(); + // We cannot use `WorkerLinter` on Node.js since it relies on web-specific APIs. + let linter = new harper.LocalLinter(); - let lints = await linter.lint('This is a example of how to use `harper.js`.'); + 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:'); + 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()); + for (let lint of lints) { + console.log(" - ", lint.span().start, ":", lint.span().end, lint.message()); - if (lint.suggestion_count() != 0) { - console.log('Suggestions:'); + 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() - ); - } - } - } + for (let sug of lint.suggestions()) { + console.log( + "\t - ", + sug.kind() == 1 ? "Remove" : "Replace with", + sug.get_replacement_text(), + ); + } + } + } } main(); From 8a8182210b9679e465da0a6f5b1677a739712d02 Mon Sep 17 00:00:00 2001 From: aminwhat Date: Thu, 2 Jan 2025 12:50:23 +0330 Subject: [PATCH 3/7] fix spacing --- .../docs/harperjs/introduction/+page.md | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/packages/web/src/routes/docs/harperjs/introduction/+page.md b/packages/web/src/routes/docs/harperjs/introduction/+page.md index c128f95d..32c74252 100644 --- a/packages/web/src/routes/docs/harperjs/introduction/+page.md +++ b/packages/web/src/routes/docs/harperjs/introduction/+page.md @@ -25,31 +25,32 @@ It can be imported [natively in a browser](./CDN) or through [npm](https://www.n ## Sample Usage: ```js -let harper = require('harper.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(); + // We cannot use `WorkerLinter` on Node.js since it relies on web-specific APIs. + let linter = new harper.LocalLinter(); - let lints = await linter.lint('This is a example of how to use `harper.js`.'); + 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:'); + 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()); + for (let lint of lints) { + console.log(" - ", lint.span().start, ":", lint.span().end, lint.message()); - if (lint.suggestion_count() != 0) { - console.log('Suggestions:'); + 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() - ); - } - } - } + for (let sug of lint.suggestions()) { + console.log( + "\t - ", + sug.kind() == 1 ? "Remove" : "Replace with", + sug.get_replacement_text(), + ); + } + } + } } main(); From 69bd31749ffd1c8b38513a327530141aad154987 Mon Sep 17 00:00:00 2001 From: AminWhat <88392440+aminwhat@users.noreply.github.com> Date: Thu, 2 Jan 2025 13:02:08 +0330 Subject: [PATCH 4/7] Update +page.md add .setup() --- packages/web/src/routes/docs/harperjs/introduction/+page.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/web/src/routes/docs/harperjs/introduction/+page.md b/packages/web/src/routes/docs/harperjs/introduction/+page.md index 32c74252..e363dd38 100644 --- a/packages/web/src/routes/docs/harperjs/introduction/+page.md +++ b/packages/web/src/routes/docs/harperjs/introduction/+page.md @@ -31,6 +31,7 @@ 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`."); From d804e468de2188228d9ee0a04b912aaf883ac99a Mon Sep 17 00:00:00 2001 From: Elijah Potter Date: Thu, 2 Jan 2025 08:21:03 -0700 Subject: [PATCH 5/7] fix(web): ran `just format` to appease Prettier --- .../docs/harperjs/introduction/+page.md | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/web/src/routes/docs/harperjs/introduction/+page.md b/packages/web/src/routes/docs/harperjs/introduction/+page.md index 277711f9..49e503f5 100644 --- a/packages/web/src/routes/docs/harperjs/introduction/+page.md +++ b/packages/web/src/routes/docs/harperjs/introduction/+page.md @@ -22,35 +22,35 @@ It can be imported [natively in a browser](./CDN) or through [npm](https://www.n @install-pkg(harper.js) - ## Sample Usage: + ```js -let harper = require("harper.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(); + // 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`."); + 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:"); + 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()); + for (let lint of lints) { + console.log(' - ', lint.span().start, ':', lint.span().end, lint.message()); - if (lint.suggestion_count() != 0) { - console.log("Suggestions:"); + 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(), - ); - } - } - } + for (let sug of lint.suggestions()) { + console.log( + '\t - ', + sug.kind() == 1 ? 'Remove' : 'Replace with', + sug.get_replacement_text() + ); + } + } + } } main(); From 2075d2c77760e3dea7aaa28671a149def9c6f01d Mon Sep 17 00:00:00 2001 From: Elijah Potter Date: Thu, 2 Jan 2025 08:34:39 -0700 Subject: [PATCH 6/7] feat(web): moved the Node.js example to its own page --- .../commonjs-simple/{index.cjs => index.js} | 0 .../examples/commonjs-simple/package.json | 2 +- .../docs/harperjs/introduction/+page.md | 36 +------------------ .../src/routes/docs/harperjs/node/+page.md | 15 ++++++++ packages/web/vite.config.ts | 4 +++ 5 files changed, 21 insertions(+), 36 deletions(-) rename packages/harper.js/examples/commonjs-simple/{index.cjs => index.js} (100%) create mode 100644 packages/web/src/routes/docs/harperjs/node/+page.md diff --git a/packages/harper.js/examples/commonjs-simple/index.cjs b/packages/harper.js/examples/commonjs-simple/index.js similarity index 100% rename from packages/harper.js/examples/commonjs-simple/index.cjs rename to packages/harper.js/examples/commonjs-simple/index.js diff --git a/packages/harper.js/examples/commonjs-simple/package.json b/packages/harper.js/examples/commonjs-simple/package.json index 74bd5808..5c5d138a 100644 --- a/packages/harper.js/examples/commonjs-simple/package.json +++ b/packages/harper.js/examples/commonjs-simple/package.json @@ -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" diff --git a/packages/web/src/routes/docs/harperjs/introduction/+page.md b/packages/web/src/routes/docs/harperjs/introduction/+page.md index 49e503f5..f2cc9b9e 100644 --- a/packages/web/src/routes/docs/harperjs/introduction/+page.md +++ b/packages/web/src/routes/docs/harperjs/introduction/+page.md @@ -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(); -``` diff --git a/packages/web/src/routes/docs/harperjs/node/+page.md b/packages/web/src/routes/docs/harperjs/node/+page.md new file mode 100644 index 00000000..ef9ca2de --- /dev/null +++ b/packages/web/src/routes/docs/harperjs/node/+page.md @@ -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) diff --git a/packages/web/vite.config.ts b/packages/web/vite.config.ts index 53c76f39..8f5adb17 100644 --- a/packages/web/vite.config.ts +++ b/packages/web/vite.config.ts @@ -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' From 613d0a2eae2b9af0f658e965751ad081e9df659d Mon Sep 17 00:00:00 2001 From: Elijah Potter Date: Thu, 2 Jan 2025 08:38:43 -0700 Subject: [PATCH 7/7] fix: satisfy `eslint` and update `harper.js` version --- packages/harper.js/examples/commonjs-simple/index.js | 2 +- packages/harper.js/examples/commonjs-simple/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/harper.js/examples/commonjs-simple/index.js b/packages/harper.js/examples/commonjs-simple/index.js index 2a27a5ef..69bba9e6 100644 --- a/packages/harper.js/examples/commonjs-simple/index.js +++ b/packages/harper.js/examples/commonjs-simple/index.js @@ -1,4 +1,4 @@ -let harper = require('harper.js'); +import * as harper from 'harper.js'; async function main() { // We cannot use `WorkerLinter` on Node.js since it relies on web-specific APIs. diff --git a/packages/harper.js/examples/commonjs-simple/package.json b/packages/harper.js/examples/commonjs-simple/package.json index 5c5d138a..ad222da0 100644 --- a/packages/harper.js/examples/commonjs-simple/package.json +++ b/packages/harper.js/examples/commonjs-simple/package.json @@ -6,6 +6,6 @@ "start": "node index.js" }, "dependencies": { - "harper.js": "^0.13.0" + "harper.js": "^0.14.0" } }