@@ -2,6 +2,7 @@ import React, { useState } from "react";
2
2
import { Button } from "react-bootstrap" ;
3
3
import { Tooltip } from "react-bootstrap" ;
4
4
import { OverlayTrigger } from "react-bootstrap" ;
5
+ import { useLocation } from "@gatsbyjs/reach-router" ;
5
6
6
7
const childToString = ( child ) => {
7
8
if ( typeof child === "string" ) {
@@ -22,13 +23,13 @@ const popExtraNewLines = (code) => {
22
23
}
23
24
} ;
24
25
25
- const splitChildrenIntoCodeAndOutput = ( rawChildren ) => {
26
+ const splitChildrenIntoCodeAndOutput = ( rawChildren , urlpath ) => {
26
27
if ( ! rawChildren ) {
27
28
return [ [ ] , [ ] ] ;
28
29
}
29
30
30
31
// Simplified regex to split on the __OUTPUT__ marker
31
- const splitRegex = / \n _ _ O U T P U T _ _ \n / ;
32
+ const splitRegex = / (?: \n | ^ ) [ \t \f \v ] * _ _ O U T P U T _ _ [ \t \f \v ] * (?: \n | $ ) / ;
32
33
const code = [ ] ;
33
34
const output = [ ] ;
34
35
@@ -41,6 +42,12 @@ const splitChildrenIntoCodeAndOutput = (rawChildren) => {
41
42
if ( splitFound ) {
42
43
// we've already split, toss it into output and move on
43
44
output . push ( childToString ( child ) ) ;
45
+ // warn if we do find another __OUTPUT__ marker - this is likely a mistake
46
+ if ( splitRegex . test ( output . at ( - 1 ) ) )
47
+ console . warn (
48
+ urlpath +
49
+ ": Found another __OUTPUT__ marker after the first one, this is likely a mistake." ,
50
+ ) ;
44
51
continue ;
45
52
}
46
53
@@ -152,10 +159,11 @@ const OutputPre = ({ content }) => (
152
159
) ;
153
160
154
161
const CodeBlock = ( { children, codeLanguages, ...otherProps } ) => {
162
+ const location = useLocation ( ) ;
155
163
const childIsComponent = ! ! children . props ; // true in normal usage, false if raw <pre> tags are used
156
164
157
165
const [ codeContent , outputContent ] = childIsComponent
158
- ? splitChildrenIntoCodeAndOutput ( children . props . children )
166
+ ? splitChildrenIntoCodeAndOutput ( children . props . children , location . pathname )
159
167
: [ children , "" ] ;
160
168
161
169
const startWrapped = false ;
0 commit comments