DEV: implements parseAsync in discourse/lib/text (#17899)

`parseAsync` allows to parse a block of markdown into tokens.

Usage:

```javascript
import { parseAsync } from "discourse/lib/text";

// ...

await parseAsync("**test**").then((tokens) => {
 console.log(tokens);
})
```
This commit is contained in:
Joffrey JAFFEUX 2022-08-13 14:25:32 +02:00 committed by GitHub
parent 3dac4fe075
commit 7476c22324
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -68,6 +68,12 @@ export function sanitizeAsync(text, options) {
}); });
} }
export function parseAsync(md, options) {
return loadMarkdownIt().then(() => {
return createPrettyText(options).opts.engine.parse(md);
});
}
function loadMarkdownIt() { function loadMarkdownIt() {
return new Promise((resolve) => { return new Promise((resolve) => {
let markdownItURL = Session.currentProp("markdownItURL"); let markdownItURL = Session.currentProp("markdownItURL");

View File

@ -0,0 +1,14 @@
import { module, test } from "qunit";
import { parseAsync } from "discourse/lib/text";
module("Unit | Utility | text", function () {
test("parseAsync", async function (assert) {
await parseAsync("**test**").then((tokens) => {
assert.strictEqual(
tokens[1].children[1].type,
"strong_open",
"it parses the raw markdown"
);
});
});
});