|
| 1 | +import { createMarkdown, stripMarkdown } from '@deepnote/blocks'; |
1 | 2 | import { NotebookCellData, NotebookCellKind } from 'vscode'; |
2 | 3 |
|
3 | 4 | import type { BlockConverter } from './blockConverter'; |
4 | 5 | import type { DeepnoteBlock } from '../deepnoteTypes'; |
5 | 6 |
|
6 | 7 | export class TextBlockConverter implements BlockConverter { |
7 | | - protected static readonly textBlockTypes = ['text-cell-h1', 'text-cell-h2', 'text-cell-h3', 'text-cell-p']; |
| 8 | + protected static readonly textBlockTypes = [ |
| 9 | + 'text-cell-h1', |
| 10 | + 'text-cell-h2', |
| 11 | + 'text-cell-h3', |
| 12 | + 'text-cell-p', |
| 13 | + 'text-cell-bullet', |
| 14 | + 'text-cell-todo', |
| 15 | + 'text-cell-callout', |
| 16 | + 'separator' |
| 17 | + ]; |
8 | 18 |
|
9 | 19 | applyChangesToBlock(block: DeepnoteBlock, cell: NotebookCellData): void { |
10 | | - let value = cell.value || ''; |
11 | | - |
12 | | - if (block.type === 'text-cell-h1') { |
13 | | - value = value.replace(/^\s*#\s+/, ''); |
14 | | - } else if (block.type === 'text-cell-h2') { |
15 | | - value = value.replace(/^\s*##\s+/, ''); |
16 | | - } else if (block.type === 'text-cell-h3') { |
17 | | - value = value.replace(/^\s*###\s+/, ''); |
| 20 | + // For separator, just keep empty content |
| 21 | + if (block.type === 'separator') { |
| 22 | + block.content = ''; |
| 23 | + |
| 24 | + return; |
18 | 25 | } |
19 | 26 |
|
20 | | - block.content = value; |
| 27 | + // Update block content with cell value first |
| 28 | + block.content = cell.value || ''; |
| 29 | + |
| 30 | + // Then strip the markdown formatting to get plain text |
| 31 | + const textValue = stripMarkdown(block); |
| 32 | + |
| 33 | + block.content = textValue; |
21 | 34 | } |
22 | 35 |
|
23 | 36 | canConvert(blockType: string): boolean { |
24 | 37 | return TextBlockConverter.textBlockTypes.includes(blockType.toLowerCase()); |
25 | 38 | } |
26 | 39 |
|
27 | 40 | convertToCell(block: DeepnoteBlock): NotebookCellData { |
28 | | - // TODO: Use the library to handle the markdown conversion here in the future. |
29 | | - |
30 | | - let value = block.content || ''; |
31 | | - |
32 | | - if (block.type === 'text-cell-h1') { |
33 | | - value = `# ${value}`; |
34 | | - } else if (block.type === 'text-cell-h2') { |
35 | | - value = `## ${value}`; |
36 | | - } else if (block.type === 'text-cell-h3') { |
37 | | - value = `### ${value}`; |
38 | | - } |
| 41 | + const markdown = createMarkdown(block); |
39 | 42 |
|
40 | | - const cell = new NotebookCellData(NotebookCellKind.Markup, value, 'markdown'); |
| 43 | + const cell = new NotebookCellData(NotebookCellKind.Markup, markdown, 'markdown'); |
41 | 44 |
|
42 | 45 | return cell; |
43 | 46 | } |
|
0 commit comments