1 1
import { parseComponent } from 'vue-template-compiler'
2 1
import { isCodeVueSfc } from 'vue-inbrowser-compiler-utils'
3

4 1
export default function (code: string, jsxInExamples: boolean): string {
5
	// In case we are loading a vue component as an example, extract script tag
6 1
	if (isCodeVueSfc(code)) {
7 1
		const parts = parseComponent(code)
8 1
		return parts && parts.script ? parts.script.content : ''
9
	}
10

11
	// if in JSX mode or litteral return examples code as is
12 1
	if (jsxInExamples || /new Vue\(/.test(code)) {
13 1
		return code
14
	}
15

16 1
	if (/\n\W+?export\W+default\W/.test(code) || /\n\W+?module.exports(\W+)?=/.test(code)) {
17 1
		return code
18
	}
19

20
	//else it could be the weird almost jsx of vue-styleguidist
21 1
	return /^</.test(code.trim()) ? '' : code.split(/\n[\t ]*</)[0]
22
}

Read our documentation on viewing source code .

Loading