Skip to content

Commit 44576d5

Browse files
committed
feat: enhance markdown block controller to support dynamic locale and route scope path
1 parent 76aa680 commit 44576d5

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

app/javascript/controllers/better_together/markdown_block_controller.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ export default class extends Controller {
1717
this.previousSourceType = this.selectedSourceType
1818
}
1919

20+
get locale() {
21+
try {
22+
if (typeof I18n !== 'undefined' && I18n && I18n.locale) return I18n.locale
23+
} catch (e) {}
24+
try {
25+
const htmlLang = document.documentElement.getAttribute('lang')
26+
if (htmlLang) return htmlLang
27+
} catch (e) {}
28+
return 'en'
29+
}
30+
31+
get routeScopePath() {
32+
try {
33+
if (typeof BetterTogether !== 'undefined' && BetterTogether && BetterTogether.route_scope_path) return BetterTogether.route_scope_path
34+
} catch (e) {}
35+
try {
36+
if (this.element && this.element.dataset && this.element.dataset.routeScopePath) return this.element.dataset.routeScopePath
37+
} catch (e) {}
38+
return ''
39+
}
40+
2041
handleSourceTypeChange() {
2142
const selectedType = this.selectedSourceType
2243
if (!selectedType) return
@@ -85,9 +106,11 @@ export default class extends Controller {
85106
}
86107

87108
async renderMarkdown(content) {
88-
// Make an AJAX request to render the markdown on the server
89-
const locale = document.documentElement.lang || 'en'
90-
const response = await fetch(`/${locale}/content/blocks/preview_markdown`, {
109+
// Make an AJAX request to render the markdown on the server
110+
const locale = this.locale
111+
const routeScope = this.routeScopePath
112+
const scopeSegment = routeScope ? `/${routeScope}` : ''
113+
const response = await fetch(`/${locale}${scopeSegment}/content/blocks/preview_markdown`, {
91114
method: 'POST',
92115
headers: {
93116
'Content-Type': 'application/json',

spec/requests/better_together/content_blocks_preview_markdown_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
RSpec.describe 'Content Blocks Markdown Preview', :as_user do
66
let(:markdown_content) { "# Hello World\n\nThis is a **test**." }
7-
let(:preview_path) { "/#{I18n.default_locale}/content/blocks/preview_markdown" }
7+
let(:preview_path) { "/#{I18n.default_locale}/#{BetterTogether.route_scope_path}/content/blocks/preview_markdown" }
88

99
describe 'POST /better_together/content/blocks/preview_markdown' do
1010
context 'when markdown content is provided' do

0 commit comments

Comments
 (0)