I'm finally getting around to doing a simple admin setting. I have it showing in the admin, and I can save data in the field. But, I'm having trouble using the setting, in this case a regular expression, in the frontend. Does the following look correct as far as how you would make a setting, then get it to show in the frontend?
This is for a potential pull request for @Justoverclock's hashtag extension.
Admin setting (which shows up and saves so this part appears to work fine):
import app from "flarum/admin/app";
app.initializers.add("justoverclock/flarum-ext-hashtag", () => {
app.extensionData.for("justoverclock-hashtag").registerSetting(
{
setting: "justoverclock-hashtag.regex",
name: "regex",
type: "text",
required: true,
label: app.translator.trans(
"justoverclock-hashtag.admin.settings.regexlabel"
),
},
15
);
});
Component for frontend which will work if I manually put the regular expression as the regex const. But, I want to pull it from the database instead. I'm specifically talking about line 4 const regex = app.forum.attribute('regex');
. That's the only part not working.
import app from 'flarum/forum/app';
export default function () {
const regex = app.forum.attribute('regex');
const p = this.$('p');
const discussionsUrl = app.route('index');
const tooltip = app.translator.trans('justoverclock-hashtag.forum.post.hashtag_link_tooltip');
p.each((index, element) => {
$(element).html(
$(element)
.html()
.replace(regex, (match) => ` <a href="${discussionsUrl}?q=${match.replace(/#/g,'').trim()}" class="hasht" title="${tooltip}">${match.trim()}</a>`)
);
setTimeout(() => {
$(element)
.find('a.hasht')
.click((e) => {
if (e.ctrlKey || e.metaKey || e.which === 2) return;
e.preventDefault();
m.route.set(e.target.href);
});
}, 1);
});
}