Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions FrontEnd/scripts/controllers/readme_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,41 @@
// limitations under the License.

import { Controller } from '@hotwired/stimulus'
import mermaid from 'mermaid'

export class ReadmeController extends Controller {
frameLoaded() {
this.navigateToAnchorFromLocation()
this.renderMermaidDiagrams()
}

async renderMermaidDiagrams() {
// Replace all Mermaid chart sources with rendered diagrams.
const mermaidSectionElements = document.querySelectorAll('section[data-type="mermaid"]')
for (const [index, mermaidSectionElement] of Array.from(mermaidSectionElements).entries()) {
// No need to parse the JSON, the chart source is in a `data-plain` attribute.
const mermaidDataElement = mermaidSectionElement.querySelector('[data-plain]')
if (!mermaidDataElement) continue
const chartDefinition = mermaidDataElement.getAttribute('data-plain')
if (!chartDefinition) continue

// Make a container with *both* light and dark charts.
const chartContainer = document.createElement('div')
chartContainer.classList.add('mermaid-chart')
mermaidDataElement.appendChild(chartContainer)

// The documentation says not to call `initialize` more than once. That said, it's
// the only way to switch themes and therefore the only way to get this working.
mermaid.initialize({ theme: 'default', nodeSpacing: 50, rankSpacing: 50, curve: 'basis' })
const lightRenderResult = await mermaid.render(`mermaid-chart-light-${index}`, chartDefinition)
chartContainer.insertAdjacentHTML('beforeend', lightRenderResult.svg)

mermaid.initialize({ theme: 'dark', nodeSpacing: 50, rankSpacing: 50, curve: 'basis' })
const darkRenderResult = await mermaid.render(`mermaid-chart-dark-${index}`, chartDefinition)
chartContainer.insertAdjacentHTML('beforeend', darkRenderResult.svg)
}
}

navigateToAnchorFromLocation() {
// If the browser has an anchor in the URL that may be inside the README then
// we should attempt to scroll it into view once the README is loaded.
Expand Down
25 changes: 25 additions & 0 deletions FrontEnd/styles/readme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,31 @@
.markdown-alert-caution {
color: var(--mid-red);
}

.mermaid-chart {
display: flex;
justify-content: center;
padding: 30px 0;
}

section[data-type='mermaid'] {
pre,
.js-render-enrichment-loader {
display: none;
}

@media (prefers-color-scheme: light) {
[id^='mermaid-chart-dark'] {
display: none;
}
}

@media (prefers-color-scheme: dark) {
[id^='mermaid-chart-light'] {
display: none;
}
}
}
}

g-emoji {
Expand Down
2 changes: 1 addition & 1 deletion Sources/App/Views/PackageController/PackageShow+View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ extension PackageShow {
.value(model.repositoryName),
.readme).relativeURL(),
.data(named: "controller", value: "readme"),
.data(named: "action", value: "turbo:frame-load->readme#navigateToAnchorFromLocation"),
.data(named: "action", value: "turbo:frame-load->readme#frameLoaded"),
.div(.spinner())
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/owner/repo/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/owner/repo/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ <h3>Compatibility</h3>
</ul>
</nav>
<section data-tab-bar-target="content">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#navigateToAnchorFromLocation">
<turbo-frame id="readme_content" src="/Alamo/Alamofire/readme" data-controller="readme" data-action="turbo:frame-load->readme#frameLoaded">
<div>
<div class="spinner">
<div class="rect1"></div>
Expand Down
Loading
Loading