b5e24d0344541e355292a7127f49804494bb341cd31deac5e7906d3fbadebc3bd0645023b13e3cba22feada638e18e246dd3a6fa0ced0fc26d462553b2a1dd 951 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* @flow */
  2. const compile = require('lodash.template')
  3. const compileOptions = {
  4. escape: /{{([^{][\s\S]+?[^}])}}/g,
  5. interpolate: /{{{([\s\S]+?)}}}/g
  6. }
  7. export type ParsedTemplate = {
  8. head: (data: any) => string;
  9. neck: (data: any) => string;
  10. tail: (data: any) => string;
  11. };
  12. export function parseTemplate (
  13. template: string,
  14. contentPlaceholder?: string = '<!--vue-ssr-outlet-->'
  15. ): ParsedTemplate {
  16. if (typeof template === 'object') {
  17. return template
  18. }
  19. let i = template.indexOf('</head>')
  20. const j = template.indexOf(contentPlaceholder)
  21. if (j < 0) {
  22. throw new Error(`Content placeholder not found in template.`)
  23. }
  24. if (i < 0) {
  25. i = template.indexOf('<body>')
  26. if (i < 0) {
  27. i = j
  28. }
  29. }
  30. return {
  31. head: compile(template.slice(0, i), compileOptions),
  32. neck: compile(template.slice(i, j), compileOptions),
  33. tail: compile(template.slice(j + contentPlaceholder.length), compileOptions)
  34. }
  35. }