Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown mermaid #2

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,25 @@ const ipcRenderer = window.ipc
*
* @return {string} The URL string, or undefined
*/
function getURLForNode (node: SyntaxNode, state: EditorState): string|undefined {
if (node.type.name === 'URL') {
return state.sliceDoc(node.from, node.to)
}
function getURLForNode(node: SyntaxNode, state: EditorState): string | undefined {
// Check if the node itself is of type 'URL'
if (node.type.name === 'URL') {
return state.sliceDoc(node.from, node.to);
}

const child = node.getChild('URL')
// If the node has children, recursively search for a node of type 'URL'
if (node.firstChild !== null) {
for (let child of node.children) {
const url = getURLForNode(child, state);
if (url !== undefined) {
return url;
}
}
}

if (child === null) {
return undefined
} else {
return state.sliceDoc(child.from, child.to)
}
// If no 'URL' node is found in the children, return undefined
return undefined;
}

/**
* Shows a context menu appropriate for a link or image using the given node
*
Expand Down
10 changes: 10 additions & 0 deletions source/renderer/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import MarkdownIt from 'markdown-it';
import MarkdownItCustomBlock from 'markdown-it-custom-block';

const md = new MarkdownIt();

md.use(MarkdownItCustomBlock, {
chart: function (content: string) {
return `<canvas class="chartjs-render" data-config='${content}'></canvas>`;
}
});
5 changes: 5 additions & 0 deletions source/renderer/renderChart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Chart from 'chart.js/auto';

export function renderChart(ctx: CanvasRenderingContext2D, config: any) {
new Chart(ctx, config);
}
25 changes: 25 additions & 0 deletions source/renderer/renderer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { renderChart } from './renderChart';
import MarkdownIt from 'markdown-it';
import MarkdownItCustomBlock from 'markdown-it-custom-block';

const md = new MarkdownIt();
md.use(MarkdownItCustomBlock, {
chart: function (content: string) {
return `<canvas class="chartjs-render" data-config='${content}'></canvas>`;
}
});

export function renderMarkdownWithCharts(markdown: string): string {
const html = md.render(markdown);
const container = document.getElementById('markdown-container');
if (container) {
container.innerHTML = html;
const chartBlocks = container.querySelectorAll('.chartjs-render');
chartBlocks.forEach(block => {
const config = JSON.parse(block.getAttribute('data-config') || '{}');
const canvas = block as HTMLCanvasElement;
renderChart(canvas.getContext('2d')!, config);
});
}
return html;
}