これをフォローすると、“wrap_open” BBCode トークンを使用するプラグインがありますが、リッチテキストエディタではまだサポートされていません。そのため、現時点では Markdown モードに留まる必要があります。
これを確実にするために、Claude Sonnet を使用して以下のコードを生成しました。改善点があれば(特にバグがあれば
)、フィードバックをいただけると幸いです。他のフォーラム管理者にも役立つかもしれないので、共有します。コードは「テーマ設定」>「JS」(/admin/customize/themes/2/common/js/edit)に入力します。
import {
apiInitializer
} from "discourse/lib/api";
export default apiInitializer((api) => {
// ユーザーのエディタを強制的に Markdown モードに切り替える
function switchToMarkdownMode() {
const currentUser = api.getCurrentUser();
// コンポーザーが開かれた場合、ユーザーはサインインしているはずです
if (!currentUser) {
console.error('ユーザーが見つかりません');
return;
}
// ここからオプションを取得しました
// 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) {
// Markdown モードでない場合のみ切り替える
// 使用例: https://github.com/discourse/discourse/blob/87476ce2c18fb8f856dda7ff03804ed5fbb0ff38/app/assets/javascripts/discourse/app/services/user-tips.js#L127
currentUser.set('user_option.composition_mode', 0);
// ユーザー設定をサーバーに保存する
currentUser.save(['composition_mode']).then(() => {
console.log('Markdown モードに正常に切り替わりました');
}).catch((error) => {
console.error('コンポジションモードの更新に失敗しました:', error);
});
// 現在のコンポーザー 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('現在のコンポーザーを Markdown モードに切り替えました');
}
}
}, 100); // コンポーザーが完全にレンダリングされるまで少し待機する
} else {
console.log('すでに Markdown モードです。変更は不要です');
}
}
api.onAppEvent('composer:opened', () => {
switchToMarkdownMode();
});
});