MCPcopy Index your code
hub / github.com/deepnote/deepnote / parsePercentFormat

Function parsePercentFormat

packages/convert/src/percent-to-deepnote.ts:46–150  ·  view source on GitHub ↗
(content: string)

Source from the content-addressed store, hash-verified

44 * ```
45 */
46export function parsePercentFormat(content: string): PercentNotebook {
47 const cells: PercentCell[] = []
48 const lines = content.split('\n')
49
50 // Handle empty content
51 if (lines.length === 0 || (lines.length === 1 && lines[0] === '')) {
52 return { cells: [] }
53 }
54
55 // Regular expression to match cell markers
56 // Matches: # %% [type] title tags=["a", "b"]
57 const cellMarkerRegex = /^# %%\s*(?:\[(\w+)\])?\s*(.*)$/
58
59 let currentCell: PercentCell | null = null
60 let currentContent: string[] = []
61
62 function finalizeCell() {
63 if (currentCell) {
64 // For markdown cells, strip the '# ' prefix from each line
65 if (currentCell.cellType === 'markdown') {
66 currentCell.content = currentContent
67 .map(line => {
68 if (line.startsWith('# ')) {
69 return line.slice(2)
70 }
71 if (line === '#') {
72 return ''
73 }
74 return line
75 })
76 .join('\n')
77 .trim()
78 } else {
79 currentCell.content = currentContent.join('\n').trim()
80 }
81 cells.push(currentCell)
82 }
83 currentContent = []
84 }
85
86 for (const line of lines) {
87 const match = cellMarkerRegex.exec(line)
88
89 if (match) {
90 // Finalize previous cell
91 finalizeCell()
92
93 // Parse cell type and metadata
94 const cellTypeStr = match[1]?.toLowerCase() || 'code'
95 const rest = match[2]?.trim() || ''
96
97 let cellType: 'code' | 'markdown' | 'raw' = 'code'
98 if (cellTypeStr === 'markdown' || cellTypeStr === 'md') {
99 cellType = 'markdown'
100 } else if (cellTypeStr === 'raw') {
101 cellType = 'raw'
102 }
103

Calls 1

finalizeCellFunction · 0.85

Tested by

no test coverage detected