188c88e8eSopenharmony_ci/*
288c88e8eSopenharmony_ci * Licensed to the Apache Software Foundation (ASF) under one
388c88e8eSopenharmony_ci * or more contributor license agreements.  See the NOTICE file
488c88e8eSopenharmony_ci * distributed with this work for additional information
588c88e8eSopenharmony_ci * regarding copyright ownership.  The ASF licenses this file
688c88e8eSopenharmony_ci * to you under the Apache License, Version 2.0 (the
788c88e8eSopenharmony_ci * "License"); you may not use this file except in compliance
888c88e8eSopenharmony_ci * with the License.  You may obtain a copy of the License at
988c88e8eSopenharmony_ci *
1088c88e8eSopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
1188c88e8eSopenharmony_ci *
1288c88e8eSopenharmony_ci * Unless required by applicable law or agreed to in writing,
1388c88e8eSopenharmony_ci * software distributed under the License is distributed on an
1488c88e8eSopenharmony_ci * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1588c88e8eSopenharmony_ci * KIND, either express or implied.  See the License for the
1688c88e8eSopenharmony_ci * specific language governing permissions and limitations
1788c88e8eSopenharmony_ci * under the License.
1888c88e8eSopenharmony_ci */
1988c88e8eSopenharmony_ci
2088c88e8eSopenharmony_ciimport loaderUtils from 'loader-utils'
2188c88e8eSopenharmony_ci
2288c88e8eSopenharmony_ciimport {
2388c88e8eSopenharmony_ci  extractBlocks
2488c88e8eSopenharmony_ci} from './parser'
2588c88e8eSopenharmony_ci
2688c88e8eSopenharmony_cimodule.exports = function (source) {
2788c88e8eSopenharmony_ci  this.cacheable && this.cacheable()
2888c88e8eSopenharmony_ci
2988c88e8eSopenharmony_ci  const callback = this.async()
3088c88e8eSopenharmony_ci  const loaderQuery = loaderUtils.getOptions(this) || {}
3188c88e8eSopenharmony_ci  const resourceQuery = loaderUtils.parseQuery(this.resourceQuery) || {}
3288c88e8eSopenharmony_ci  const name = resourceQuery.name
3388c88e8eSopenharmony_ci
3488c88e8eSopenharmony_ci  let contentPromise
3588c88e8eSopenharmony_ci
3688c88e8eSopenharmony_ci  if (loaderQuery.extract) {
3788c88e8eSopenharmony_ci    contentPromise =
3888c88e8eSopenharmony_ci      extractBlocks(source, 'elements')
3988c88e8eSopenharmony_ci  }
4088c88e8eSopenharmony_ci  else {
4188c88e8eSopenharmony_ci    contentPromise = Promise.resolve({ content: source })
4288c88e8eSopenharmony_ci  }
4388c88e8eSopenharmony_ci
4488c88e8eSopenharmony_ci  contentPromise.then(elements => {
4588c88e8eSopenharmony_ci    if (loaderQuery.raw) {
4688c88e8eSopenharmony_ci      return elements[name].content
4788c88e8eSopenharmony_ci    }
4888c88e8eSopenharmony_ci  }).then(result => {
4988c88e8eSopenharmony_ci    callback(null, result)
5088c88e8eSopenharmony_ci  }).catch(e => {
5188c88e8eSopenharmony_ci    callback(e, '')
5288c88e8eSopenharmony_ci  })
5388c88e8eSopenharmony_ci}
54