Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: addPrettyTextAdditionalOptions plugin API #15727

Conversation

martin-brennan
Copy link
Contributor

When writing markdown rules and features, additional options not part of
the main options payload may be needed for the rule. Rules cannot access
things like the Site or current User objects which can have serialized data
which may be useful when rendering markdown rules.

const site = api.container.lookup("site:main");
api.addPrettyTextAdditionalOptions({
 someSiteOption: site.some_option
})

These options are passed down to several of the markdown helper functions
created by discourse-markdown-it extensions, such as registerPlugin and registerOptions,
and can be accessed like so:

opts.discourse.additionalOptions.someSiteOption

These options are held in a separate additionalOptions namespace because
they are not meant to override the core pretty-text options.


The main motivation for adding this is the chat plugin, which stores
chat_pretty_text_features and chat_pretty_text_markdown_rules on
the Site object via additions to the serializer, and the Site object is
not accessible to import via markdown rules (either through
Site.current() or through container.lookup)

When writing markdown rules and features, additional options not part of
the main options payload may be needed for the rule. Rules cannot access
things like the Site or current User objects which can have serialized data
which may be useful when rendering markdown rules.

```
const site = api.container.lookup("site:main");
api.addPrettyTextAdditionalOptions({
 someSiteOption: site.some_option
})
```

These options are passed down to several of the markdown helper functions
created by discourse-markdown-it extensions, such as registerPlugin and registerOptions,
and can be accessed like so:

```
opts.discourse.additionalOptions.someSiteOption
```

These options are held in a separate additionalOptions namespace because
they are _not_ meant to override the core pretty-text options.

-----

The main motivation for adding this is the chat plugin, which stores
`chat_pretty_text_features` and `chat_pretty_text_markdown_rules` on
the Site object via additions to the serializer, and the Site object is
not accessible to import via markdown rules (either through
Site.current() or through container.lookup)
addPrettyTextOptions("pluginA", { someOption: "test" });
addPrettyTextOptions("pluginB", { doorNumberTwo: "other thing" });
const prettyText = createPrettyText();
assert.equal(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.equal(
assert.strictEqual(

prettyText.opts.discourse.additionalOptions.pluginA.someOption,
"test"
);
assert.equal(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.equal(
assert.strictEqual(

### Added
- Adds `addPrettyTextAdditionalOptions`. When writing markdown rules and features,
additional options not part of the main options payload may be needed for the rule.
Rules cannot access things like the Site or current User objects which can have
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think current User is part of the options so perhaps we should omit the current User objects?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes I see, we can leave that out.

@tgxworld
Copy link
Contributor

tgxworld commented Jan 27, 2022

For your use case here, will import Site from "discourse/models/site work? That would allow us to avoid an additional API.

O nevermind, I tried this and everything exploded on the server side. It looks like this plugin API we're adding is on the client side, how will we handle the server side which does the cooking too?

@martin-brennan
Copy link
Contributor Author

@tgxworld no, see:

The main motivation for adding this is the chat plugin, which stores
chat_pretty_text_features and chat_pretty_text_markdown_rules on
the Site object via additions to the serializer, and the Site object is
not accessible to import via markdown rules (either through
Site.current() or through container.lookup)

I wouldn't need this if I could just do that :)

@martin-brennan
Copy link
Contributor Author

how will we handle the server side which does the cooking too?

Ah damn this is a problem, I didn't realise that the text.js part is client side only, will have a think 🤔

@martin-brennan
Copy link
Contributor Author

Not the right solution, will open another PR

@martin-brennan martin-brennan deleted the dev/add-plugin-api-for-additional-pretty-text-options branch August 2, 2023 00:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants