11import { visit } from "unist-util-visit" ;
22import { toHtml } from "hast-util-to-html" ;
3- import { kebabCase } from "lodash-es" ;
3+ import { kebabCase , repeat } from "lodash-es" ;
44import { html } from "../htm-rehype.js" ;
55
66/** @typedef {Code & {tab: string, tabId: string} } TabbedCode */
77
8-
9- /**
10- * @typedef {Function } Processor
11- * @param {Root } tree
12- * @param {import('vfile').VFile } [vfile]
13- */
14-
158/** @returns {Processor } */
169export default function tabbedCodeBlock ( ) {
17-
18-
19- /**
20- * @param {Element } node
21- * @return {[Element, Element] | null }
22- */
23- function getTextAndPre ( node ) {
24- if ( node . tagName !== 'li' || ! node . children ) {
25- return null ;
26- }
27- let text = null ;
28- let pre = null ;
29- for ( let child of node . children ) {
30- if ( child . tagName === 'p' ) {
31- text = child ;
32- } else if ( child . tagName === 'pre' ) {
33- pre = child ;
34- }
35- }
36- return text && pre && [ text , pre ] ;
37- }
38-
3910 /** @param {Code|undefined } node */
4011 function isTabbedCodeBlock ( node ) {
4112 return node ?. type === 'code' && node . meta ;
@@ -58,7 +29,7 @@ export default function tabbedCodeBlock() {
5829 /** @type {Html } */
5930 const elem = { type : 'html' } ;
6031 /** @type {TabbedCode[] } */
61- const tabs = parent . children . splice ( firstTab , index - firstTab + 1 , elem ) ;
32+ const tabs = parent . children . slice ( firstTab , index + 1 ) ;
6233 const prefix = `code-${ tabs [ 0 ] . position ?. start ?. offset } ` ;
6334 elem . value = toHtml ( html `
6435 < div >
@@ -88,6 +59,9 @@ export default function tabbedCodeBlock() {
8859 </ div >
8960 </ div > `
9061 ) ;
62+
63+ parent . children . fill ( { value : '' } , firstTab , index ) ;
64+ parent . children [ index ] = elem ;
9165 } ) ;
9266 } ;
9367}
0 commit comments