Repository: gatsbyjs/gatsby
File: packages/gatsby-remark-embed-snippet/src/index.js (JavaScript)

Code before:

  visit(markdownAST, `inlineCode`, node => {
    const { value } = node

    if (value.startsWith(`embed:`)) {
      const file = value.substr(6)
      let snippetPath = normalizePath(path.join(directory, file))

      // Embed specific lines numbers of a file
      let lines = []
      var sname = ``
      const rangePrefixIndex = snippetPath.indexOf(`#L`)
      if (rangePrefixIndex > -1) {
        const range = snippetPath.slice(rangePrefixIndex + 2)
        if (range.length === 1) {
          lines = [Number.parseInt(range, 10)]
        } else {
          lines = rangeParser.parse(range)
        }
        // Remove everything after the range prefix from file path
        snippetPath = snippetPath.slice(0, rangePrefixIndex)
      } else {
        // Check to see if there is a {snippet: "snippetName"} following the file path.
        // This syntax could support additional options in the future - for now, only
        // handle a string that contains a `snippet :` option.
        var optionIndex = snippetPath.indexOf(`{`)
        if (optionIndex > -1) {
          var optionStr = snippetPath.slice(optionIndex)
          snippetPath = snippetPath.slice(0, optionIndex)
          try {
            var optVal = JSON.parse(
              optionStr.replace(/snippet\s*:/, `"snippet":`)
            )
            if (
              typeof optVal != `undefined` &&
              typeof optVal.snippet != `undefined`
            ) {
              sname = optVal.snippet
            } else {
              throw Error(`Invalid snippet options specified: ${optionStr}`)
            }
          } catch (err) {
            throw Error(`Invalid snippet options specified: ${optionStr}`)
          }
        }
      }

      if (!fs.existsSync(snippetPath)) {
        throw Error(`Invalid snippet specified; no such file "${snippetPath}"`)
      }


Review comment (suggestion):
```suggestion
        const optionIndex = snippetPath.indexOf(`{`)
```

Code after:

  visit(markdownAST, `inlineCode`, node => {
    const { value } = node

    if (value.startsWith(`embed:`)) {
      const file = value.substr(6)
      let snippetPath = normalizePath(path.join(directory, file))

      // Embed specific lines numbers of a file
      let lines = []
      let sname = ``
      const rangePrefixIndex = snippetPath.indexOf(`#L`)
      if (rangePrefixIndex > -1) {
        const range = snippetPath.slice(rangePrefixIndex + 2)
        if (range.length === 1) {
          lines = [Number.parseInt(range, 10)]
        } else {
          lines = rangeParser.parse(range)
        }
        // Remove everything after the range prefix from file path
        snippetPath = snippetPath.slice(0, rangePrefixIndex)
      } else {
        // Check to see if there is a {snippet: "snippetName"} following the file path.
        // This syntax could support additional options in the future - for now, only
        // handle a string that contains a `snippet :` option.
        const optionIndex = snippetPath.indexOf(`{`)
        if (optionIndex > -1) {
          const optionStr = snippetPath.slice(optionIndex)
          snippetPath = snippetPath.slice(0, optionIndex)
          try {
            const optVal = JSON.parse(
              optionStr.replace(/snippet\s*:/, `"snippet":`)
            )
            if (
              typeof optVal != `undefined` &&
              typeof optVal.snippet != `undefined`
            ) {
              sname = optVal.snippet
            } else {
              throw Error(`Invalid snippet options specified: ${optionStr}`)
            }
          } catch (err) {
            throw Error(`Invalid snippet options specified: ${optionStr}`)
          }
        }
      }

      if (!fs.existsSync(snippetPath)) {
        throw Error(`Invalid snippet specified; no such file "${snippetPath}"`)
      }