@@ -141,7 +141,7 @@ import {
141141 JupyterToMarkdownResult ,
142142} from "./types.ts" ;
143143import { figuresDir , inputFilesDir } from "../render.ts" ;
144- import { lines } from "../text.ts" ;
144+ import { lines , trimEmptyLines } from "../lib /text.ts" ;
145145import { partitionYamlFrontMatter , readYamlFromMarkdown } from "../yaml.ts" ;
146146import { languagesInMarkdown } from "../../execute/engine-shared.ts" ;
147147import {
@@ -347,7 +347,7 @@ export async function quartoMdToJupyter(
347347 delete yaml . jupyter ;
348348 // write the cell only if there is metadata to write
349349 if ( Object . keys ( yaml ) . length > 0 ) {
350- const yamlFrontMatter = mdTrimEmptyLines ( lines ( stringify ( yaml , {
350+ const yamlFrontMatter = trimEmptyLines ( lines ( stringify ( yaml , {
351351 indent : 2 ,
352352 lineWidth : - 1 ,
353353 sortKeys : false ,
@@ -405,7 +405,7 @@ export async function quartoMdToJupyter(
405405 }
406406
407407 // if the source is empty then don't add it
408- cell . source = mdTrimEmptyLines ( cell . source ) ;
408+ cell . source = trimEmptyLines ( cell . source ) ;
409409 if ( cell . source . length > 0 ) {
410410 nb . cells . push ( cell ) ;
411411 }
@@ -898,7 +898,7 @@ export function jupyterCellOptionsAsComment(
898898 ...stringifyOptions ,
899899 } ) ;
900900 const commentChars = langCommentChars ( language ) ;
901- const yamlOutput = mdTrimEmptyLines ( lines ( cellYaml ) ) . map ( ( line ) => {
901+ const yamlOutput = trimEmptyLines ( lines ( cellYaml ) ) . map ( ( line ) => {
902902 line = optionCommentPrefix ( commentChars [ 0 ] ) + line +
903903 optionCommentSuffix ( commentChars [ 1 ] ) ;
904904 return line + "\n" ;
@@ -965,6 +965,26 @@ export function mdFromContentCell(
965965 return contentCellEnvelope ( cell . id , mdEnsureTrailingNewline ( source ) ) ;
966966}
967967
968+ export function mdFormatOutput ( format : string , source : string [ ] ) {
969+ const ticks = ticksForCode ( source ) ;
970+ return mdEnclosedOutput ( ticks + "{=" + format + "}" , source , ticks ) ;
971+ }
972+
973+ export function mdRawOutput ( mimeType : string , source : string [ ] ) {
974+ switch ( mimeType ) {
975+ case kTextHtml :
976+ return mdHtmlOutput ( source ) ;
977+ case kTextLatex :
978+ return mdLatexOutput ( source ) ;
979+ case kRestructuredText :
980+ return mdFormatOutput ( "rst" , source ) ;
981+ case kApplicationRtf :
982+ return mdFormatOutput ( "rtf" , source ) ;
983+ case kApplicationJavascript :
984+ return mdScriptOutput ( mimeType , source ) ;
985+ }
986+ }
987+
968988export function mdFromRawCell (
969989 cell : JupyterCellWithOptions ,
970990 options ?: JupyterToMarkdownOptions ,
@@ -973,17 +993,9 @@ export function mdFromRawCell(
973993
974994 const mimeType = cell . metadata ?. [ kCellRawMimeType ] ;
975995 if ( mimeType ) {
976- switch ( mimeType ) {
977- case kTextHtml :
978- return rawCellEnvelope ( cell . id , mdHtmlOutput ( cell . source ) ) ;
979- case kTextLatex :
980- return rawCellEnvelope ( cell . id , mdLatexOutput ( cell . source ) ) ;
981- case kRestructuredText :
982- return rawCellEnvelope ( cell . id , mdFormatOutput ( "rst" , cell . source ) ) ;
983- case kApplicationRtf :
984- return rawCellEnvelope ( cell . id , mdFormatOutput ( "rtf" , cell . source ) ) ;
985- case kApplicationJavascript :
986- return rawCellEnvelope ( cell . id , mdScriptOutput ( mimeType , cell . source ) ) ;
996+ const rawOutput = mdRawOutput ( mimeType , cell . source ) ;
997+ if ( rawOutput ) {
998+ return rawCellEnvelope ( cell . id , rawOutput ) ;
987999 }
9881000 }
9891001
@@ -1386,15 +1398,15 @@ async function mdFromCodeCell(
13861398 line . search ( / e c h o : \s + f e n c e d / ) === - 1
13871399 ) ;
13881400 if ( optionsSource . length > 0 ) {
1389- source = mdTrimEmptyLines ( source , "trailing" ) ;
1401+ source = trimEmptyLines ( source , "trailing" ) ;
13901402 } else {
1391- source = mdTrimEmptyLines ( source , "all" ) ;
1403+ source = trimEmptyLines ( source , "all" ) ;
13921404 }
13931405 source . unshift ( ...optionsSource ) ;
13941406 source . unshift ( "```{{" + options . language + "}}\n" ) ;
13951407 source . push ( "\n```\n" ) ;
13961408 } else if ( cell . optionsSource . length > 0 ) {
1397- source = mdTrimEmptyLines ( source , "leading" ) ;
1409+ source = trimEmptyLines ( source , "leading" ) ;
13981410 }
13991411 if ( options . preserveCodeCellYaml ) {
14001412 md . push ( ...cell . optionsSource ) ;
@@ -1868,11 +1880,6 @@ function mdMarkdownOutput(md: string[]) {
18681880 return md . join ( "" ) + "\n" ;
18691881}
18701882
1871- function mdFormatOutput ( format : string , source : string [ ] ) {
1872- const ticks = ticksForCode ( source ) ;
1873- return mdEnclosedOutput ( ticks + "{=" + format + "}" , source , ticks ) ;
1874- }
1875-
18761883function mdLatexOutput ( latex : string [ ] ) {
18771884 return mdFormatOutput ( "tex" , latex ) ;
18781885}
@@ -1902,36 +1909,6 @@ function mdScriptOutput(mimeType: string, script: string[]) {
19021909 return mdHtmlOutput ( scriptTag ) ;
19031910}
19041911
1905- function mdTrimEmptyLines (
1906- lines : string [ ] ,
1907- trim : "leading" | "trailing" | "all" = "all" ,
1908- ) {
1909- // trim leading lines
1910- if ( trim === "all" || trim === "leading" ) {
1911- const firstNonEmpty = lines . findIndex ( ( line ) => line . trim ( ) . length > 0 ) ;
1912- if ( firstNonEmpty === - 1 ) {
1913- return [ ] ;
1914- }
1915- lines = lines . slice ( firstNonEmpty ) ;
1916- }
1917-
1918- // trim trailing lines
1919- if ( trim === "all" || trim === "trailing" ) {
1920- let lastNonEmpty = - 1 ;
1921- for ( let i = lines . length - 1 ; i >= 0 ; i -- ) {
1922- if ( lines [ i ] . trim ( ) . length > 0 ) {
1923- lastNonEmpty = i ;
1924- break ;
1925- }
1926- }
1927- if ( lastNonEmpty > - 1 ) {
1928- lines = lines . slice ( 0 , lastNonEmpty + 1 ) ;
1929- }
1930- }
1931-
1932- return lines ;
1933- }
1934-
19351912function mdCodeOutput ( code : string [ ] , clz ?: string ) {
19361913 const ticks = ticksForCode ( code ) ;
19371914 const open = ticks + ( clz ? `{.${ clz } }` : "" ) ;
0 commit comments