Discourse.SiteSettings not working after upgrade

AI-generated summary

AstonJ is experiencing issues with Discourse.SiteSettings after an upgrade and is trying to use settings as per the upgrading guide. However, this results in an error.

After sharing the code, eviltrout explains that settings refers to theme settings, and provides a solution to access site settings using api.container.lookup('site-settings:main'). AstonJ confirms this works.

Additionally, AstonJ asks about fixing Handlebars.Utils.escapeExpression, and eviltrout suggests importing the escapeExpression helper from discourse/lib/utilities.

Following the advice here Moving away from Discourse.SiteSettings: An upgrading guide:

When I do that I get: TypeError: undefined is not an object (evaluating 'settings.tag_style')

This is in a component I created with a script in the head (none of the other options mentioned by Robin work either).

Is there anything else I can try? Should I just continue using Discourse.SiteSettings?

Can you share your code, include the HTML script tag?

Here ya go Robin:

<script type="text/discourse-plugin" version="0.1">
  const getURL = require('discourse-common/lib/get-url').default;

  function customLinkTagRenderer(tag, params) {
    params = params || {};
    const visibleName = Handlebars.Utils.escapeExpression(tag);
    tag = visibleName.toLowerCase();
    const classes = ["discourse-tag"];
    const tagName = params.tagName || "a";
    let path;
    if (tagName === "a" && !params.noHref) {
      if (params.isPrivateMessage && Discourse.User.current()) {
        const username = params.tagsForUser
          ? params.tagsForUser
          : Discourse.User.current().username;
        path = `/u/${username}/messages/tags/${tag}`;
      } else {
        path = `https://site.com/${tag}`;
      }
    }
    const href = path ? ` href='${getURL(path)}' ` : "";

    if (Discourse.SiteSettings.tag_style || params.style) {
      classes.push(params.style || Discourse.SiteSettings.tag_style);
    }

	  let val =
	    "<" +
	    tagName +
	    href +
	    " data-tag-name=" +
	    tag +
	    " class='" +
	    classes.join(" ") +
	    "'>" +
	    visibleName +
	    "</" +
	    tagName +
	    ">";

    if (params.count) {
      val += " <span class='discourse-tag-count'>x" + params.count + "</span>";
    }

    return val;
  }

  api.replaceTagRenderer(customLinkTagRenderer);
</script>

All it does is change this line:

path = `https://site.com/${tag}`;

(Handlebars.Utils.escapeExpression(tag); is not working either)

Aha, I see. settings in that case points at theme settings, which are much more common for themes/plugins.

I’ve updated the documentation with how to access site settings. You can do it like this:

<script type="text/discourse-plugin" version="0.1">
  let siteSettings = api.container.lookup('site-settings:main');
</script>

Thanks Robin - it works :smiley:

Any ideas how to fix Handlebars.Utils.escapeExpression?

You should be able to import our helper:

const { escapeExpression } = require('discourse/lib/utilities');