mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
@@ -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");
|
||||||
|
|||||||
14
app/assets/javascripts/discourse/tests/unit/lib/text-test.js
Normal file
14
app/assets/javascripts/discourse/tests/unit/lib/text-test.js
Normal 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"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user