diff --git a/lisp/ein-cell.el b/lisp/ein-cell.el index a3ab25e..438c7f5 100644 --- a/lisp/ein-cell.el +++ b/lisp/ein-cell.el @@ -203,6 +203,21 @@ a number will limit the number of lines in a cell output." (insert-image img "."))) (error (ein:log 'warn "Could not insert image: %s" err) nil))) +(defun ein:cell--markdown-heading-p (cell) + "Check if cell is behaving like a heading cell. + +Returns true if cell is a markdown cell, has at least one # character at the start of the line and is only one line long (i.e. does not contain any newline characters)." + (and (ein:markdowncell-p cell) + (string-match "^#+ " (ein:cell-get-text cell)) + (= 0 (seq-count #'(lambda (c) + (char-equal c ?\n)) + (ein:cell-get-text cell))))) + +(cl-defmethod ein:cell--markdown-heading-level ((cell ein:markdowncell)) + (progn + (string-match "^#+" (ein:cell-get-text cell)) + (match-end 0))) + ;;; Cell factory @@ -227,6 +242,7 @@ a number will limit the number of lines in a cell output." (defun ein:cell-from-type (type &rest args) (apply (ein:cell-class-from-type type) args)) + (defun ein:cell--determine-cell-type (json-data) (let ((base-type (plist-get json-data :cell_type)) (metadata (plist-get json-data :metadata))) diff --git a/lisp/ein-worksheet.el b/lisp/ein-worksheet.el index a51e482..e000ff2 100644 --- a/lisp/ein-worksheet.el +++ b/lisp/ein-worksheet.el @@ -1326,12 +1326,20 @@ function." ;; As Imenu does not provide the way to represent level *and* ;; position, use #'s to do that. (cl-loop for cell in (when (ein:worksheet-p ein:%worksheet%) - (seq-filter #'ein:headingcell-p + (seq-filter #'(lambda (cell) (or (ein:headingcell-p cell) + (ein:cell--markdown-heading-p cell))) (ein:worksheet-get-cells ein:%worksheet%))) - for sharps = (cl-loop repeat (slot-value cell 'level) collect "#") - for text = (ein:cell-get-text cell) - for name = (ein:join-str "" (append sharps (list " " text))) - collect (cons name (ein:cell-input-pos-min cell)))) + for sharps = (if (ein:headingcell-p cell) + (cl-loop repeat (slot-value cell 'level) collect "#") + (cl-loop repeat(progn + (string-match "^#+" (ein:cell-get-text cell)) + (match-end 0)) + collect "#")) + for text = (ein:cell-get-text cell) + for name = (if (ein:headingcell-p cell) + (ein:join-str "" (append sharps (list " " text))) + text) + collect (cons name (ein:cell-input-pos-min cell)))) (defun ein:worksheet-imenu-setup () "Called via notebook mode hooks."