Following up on this, we have a plugin that uses “wrap_open” BBCode tokens, which are not supported by the rich text editor yet. So we need to ensure we stay on the Markdown mode for now.
I used Claude Sonnet to generate the following code to ensure that. I would be happy to receive any feedback on how I could improve it (especially if it has any bugs ). Also sharing this in case it helps other forum admins. The code goes in Theme settings > JS (/admin/customize/themes/2/common/js/edit):
import {
apiInitializer
} from "discourse/lib/api";
export default apiInitializer((api) => {
// Force switch the user's editor to markdown mode
function switchToMarkdownMode() {
const currentUser = api.getCurrentUser();
// User should be signed in if composer was opened
if (!currentUser) {
console.error('No user found');
return;
}
// Got this option from here
// https://github.com/discourse/discourse/blob/f0fc5646dc9bd29b0e814faea490e34800e9b322/app/assets/javascripts/discourse/app/models/user.js#L262C1-L266C4
const currentMode = currentUser.get('user_option.composition_mode');
if (currentMode !== 0) {
// Only switch if not already in markdown mode
// Example usage: https://github.com/discourse/discourse/blob/87476ce2c18fb8f856dda7ff03804ed5fbb0ff38/app/assets/javascripts/discourse/app/services/user-tips.js#L127
currentUser.set('user_option.composition_mode', 0);
// Save the user preference to the server
currentUser.save(['composition_mode']).then(() => {
console.log('Successfully switched to markdown mode');
}).catch((error) => {
console.error('Failed to update composition mode:', error);
});
// Immediately toggle the current composer UI
setTimeout(() => {
const toggleButton = document.querySelector('.composer-toggle-switch[data-rich-editor]');
if (toggleButton) {
const isRichTextActive = toggleButton.getAttribute('aria-checked') === 'true';
if (isRichTextActive) {
toggleButton.click();
console.log('Toggled current composer to markdown mode');
}
}
}, 100); // Small delay to ensure composer is fully rendered
} else {
console.log('Already in markdown mode, no change needed');
}
}
api.onAppEvent('composer:opened', () => {
switchToMarkdownMode();
});
});