Skip to content

Commit

Permalink
Merge pull request #25 from jedelson-pagerduty/issue/use-class-on-cod…
Browse files Browse the repository at this point in the history
…e-block

add manual detect method. fixes #23
  • Loading branch information
johanneswuerbach authored Mar 3, 2023
2 parents c3845fa + d165b4b commit 8097a4b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,18 @@ graph TD;
C-->D;
```
~~~

## Auto-Detection vs. Manual Detection

By default, this plugin will autodetect diagrams based on the starting token of the code block. In some cases, however, this auto-detection is not sufficient, for example because of an unrecognized
diagram type or the use of front matter. In these cases, you can force the use of mermaid on blocks by adding configuration like this to your `mkdocs.yaml` file:

```yaml
markdown_extensions:
pymdownx.extra:
pymdownx.superfences:
custom_fences:
- name: mermaid
class: mermaid
format: !!python/name:pymdownx.superfences.fence_code_format
```
10 changes: 10 additions & 0 deletions src/Mermaid/Mermaid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ describe('Mermaid', () => {
expect(getByText('TEST_CONTENT')).toBeInTheDocument();
});

it.skip('renders fenced code blocks', async () => {
const { getByTestId } = await TechDocsAddonTester.buildAddonsInTechDocs([
<Mermaid config={{ themeVariables: { lineColor: '#00ff00' } }} />,
])
.withDom(<body><pre className="mermaid" data-testid="mermaid-test"><code>something here</code></pre></body>)
.renderWithEffects();

expect(getByTestId('mermaid-test')).toHaveStyle('display: none')
});

describe('selectConfig', () => {
const legacyConfig = { config: { fontFamily: 'legacy-config' } };
const lightConfig = { lightConfig: { fontFamily: 'light-config' } };
Expand Down
51 changes: 37 additions & 14 deletions src/Mermaid/Mermaid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,29 @@ export function selectConfig(backstagePalette: PaletteType, properties: MermaidP

let diagramId = 0

const makeDiagram = (el: HTMLDivElement | HTMLPreElement, diagramText: string, backstagePalette: PaletteType, properties: MermaidProps) => {
el.style.display = 'none'

const diagramElement = document.createElement('div')
diagramElement.className = "mermaid"

el.parentNode?.insertBefore(diagramElement, el.nextSibling);

const id = `mermaid-${diagramId++}`

const config = selectConfig(backstagePalette, properties);
if(config) {
mermaid.initialize(config);
}

mermaid.render(id, diagramText, (svgGraph: string) => {
diagramElement.innerHTML = svgGraph
});
}

export const MermaidAddon = (properties: MermaidProps) => {
const highlightTables = useShadowRootElements<HTMLDivElement>(['.highlighttable']);
const mermaidPreBlocks = useShadowRootElements<HTMLPreElement>(['.mermaid']);
const theme = useTheme<BackstageTheme>();

useEffect(() => {
Expand All @@ -71,25 +92,27 @@ export const MermaidAddon = (properties: MermaidProps) => {
return
}

highlightTable.style.display = 'none'

const diagramElement = document.createElement('div')
diagramElement.className = "mermaid"

highlightTable.parentNode?.insertBefore(diagramElement, highlightTable.nextSibling);
makeDiagram(highlightTable, diagramText, theme.palette.type, properties)
});
}, [highlightTables, properties, theme.palette.type]);

const id = `mermaid-${diagramId++}`
useEffect(() => {
mermaidPreBlocks.forEach(mermaidPreBlock => {
// Skip already processed
if (mermaidPreBlock.style.display === 'none') {
return
}

const config = selectConfig(theme.palette.type, properties);
if(config) {
mermaid.initialize(config);
const codeBlock = mermaidPreBlock.querySelector('code')
if (!codeBlock) {
return
}

mermaid.render(id, diagramText, (svgGraph: string) => {
diagramElement.innerHTML = svgGraph
});
const diagramText = codeBlock.innerText

makeDiagram(mermaidPreBlock, diagramText, theme.palette.type, properties)
});
}, [highlightTables, properties, theme.palette.type]);
}, [mermaidPreBlocks, properties, theme.palette.type]);

return null;
};

0 comments on commit 8097a4b

Please sign in to comment.