How to use unist-util-select - 10 common examples

To help you get started, we’ve selected a few unist-util-select examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gatsbyjs / gatsby / packages / gatsby / lib / schema / markdown.js View on Github external
_.each(files, file => {
        const page = {}
        page.src = fs.readFileSync(file, `utf-8`)
        const ast = remark.parse(page.src)
        page.ast = ast
        page.bodyHTML = thing.Compiler.prototype.compile(page.ast)
        page.headings = select(ast, `heading`).map(heading => ({
          value: _.first(select(heading, `text`).map(text => text.value)),
          depth: heading.depth,
        }))
        const parsedFrontmatter = _.first(
          select(ast, `yaml`).map(heading => yaml.load(heading.value))
        )
        const relativeDirectory = parseFilepath(
          path.relative(`${directory}/pages/`, file)
        ).dirname

        // Create path.
        let filePath
        if (_.includes(relativeDirectory, `---`)) {
          // This is for my blog bricolage.io. This will be moved out soonish.
          filePath = `/${relativeDirectory.split(`---`)[1]}/`
        } else {
          filePath = createPath(path.join(directory, `pages`), file)
        }
        // TODO put linkPrefix in gatsby.config.js and somehow get certain
        // context stuff into gatsby-helpers.js
        // TODO post issue for why manifest paths wrong
github 0xProject / 0x-monorepo / packages / website / ts / utils / algolia_helpers.ts View on Github external
function processContentTree(tree: Node[], file: File, indexName: string): void {
    const modify = modifyChildren(modifier);
    // We first modify the tree to get slugified ids from headings to all text nodes
    // This is done to be able to link to a certain section in a doc after clicking a search suggestion
    modify(tree);
    // Get all text nodes. I.e. 'heading', 'paragraph', 'list' all can have (nested) child text nodes
    const textNodes = selectAll('text', tree);

    if (textNodes) {
        // Combines text nodes that exist on the same line. I.e. if a paragraph contains 7 text nodes it combines them into 1. This makes text snippets in algolia more descriptive.
        const formattedTextNodes = formatTextNodes(textNodes);
        // Adds meta and formats information on all formatted text nodes
        const content = getContent(file, formattedTextNodes);

        const algoliaIndex = adminClient.initIndex(searchIndices[indexName]);
        const algoliaSettings = settings[indexName];

        setIndexSettings(algoliaIndex, algoliaSettings);
        void pushObjectsToAlgoliaAsync(algoliaIndex, content);
    }
}
github elmasse / nextein / src / components / content.js View on Github external
return () => /* attacher */ (tree) => {
    /* transformer */
    if (excerpt) {
      return select(selector, tree)
    }
    return tree
  }
}
github storybookjs / SBNext / server / src / components / doc.js View on Github external
return () => /* attacher */ tree => {
    /* transformer */
    tree.children = select(tree, selector);
    return tree;
  };
};
github azu / power-doctest / packages / @power-doctest / markdown / src / index.ts View on Github external
export const parse = ({ content, filePath }: ParserArgs): ParsedResults => {
    const markdownAST = attachParents(remark.parse(content));
    const codeBlocks = [].concat(
        select.selectAll(`code[lang="js"]`, markdownAST),
        select.selectAll(`code[lang="javascript"]`, markdownAST)
    );
    return codeBlocks.map((codeBlock: UnistNode) => {
        const codeValue: string = (codeBlock.value as string | undefined) || "";
        const comments = getComments(codeBlock.parent, codeBlock);
        const docTestController = new DocTestController(comments);
        const state = docTestController.state;
        const doctestOptions = docTestController.doctestOptions;
        const expectedError = docTestController.expectedErrorName;
        const metadata = docTestController.doctestMetadata;
        return {
            code: codeValue,
            location: codeBlock.position ? {
                start: {
                    line: codeBlock.position.start.line,
                    column: codeBlock.position.start.column
github adobe / jsonschema2md / test / testUtils.js View on Github external
tester.has = (selector) => {
      assert.ok(select.select(selector, node), `Markdown AST does not include node matching selector "${selector}"
${inspect(node)}`);
      return tester;
    };
    tester.equals = (selector, value) => {
github orgapp / orgajs / packages / gatsby-transformer-orga / src / orga-util.ts View on Github external
export const getProperties = headline => {
  const drawer = selectAll(`drawer`, headline).find(d => d.name === `PROPERTIES`)
  if (!drawer) return {}
  const regex = /\s*:(.+):\s*(.+)\s*$/

  return drawer.value.split(`\n`).reduce((accu, current) => {
    const m = current.match(regex)
    return { ...accu, [m[1].toLowerCase()]: m[2] }
  }, {})
}
github Symbitic / markbook / src / renderer / remark-meta.js View on Github external
return (tree, file) => {
    const heading = select('heading', tree)
    file.data.title = heading ? toString(heading) : ''

    visit(tree, ['yaml'], node => {
      file.data = {
        ...file.data,
        ...yaml(node.value)
      }
    })

    visit(tree, ['toml'], node => {
      file.data = {
        ...file.data,
        ...toml(node.value)
      }
    })
  }
github remarkjs / remark-external-links / src / index.js View on Github external
return (ast) => {
    select(ast, "link").forEach((node) => {
      if (!isRelativeUrlRegex.test(node.url)) {
        node.data = {
          hProperties: {
            ...options.target !== null &&
              {target: options.target || defaultTarget},
            ...options.rel !== null &&
              {rel: options.rel || defaultRel},
          },
        }
      }
    })
  }
}
github Alfresco / alfresco-ng2-components / tools / doc / tools / fileChecker.js View on Github external
pathnames.forEach(function (pathname) {
        var fileBaseName = path.basename(pathname, '.md');
        var tree = mdCache[pathname].mdOutTree;
        var className = ngHelpers.ngNameToClassName(fileBaseName, aggData.config.typeNameExceptions);
        var classInfo = aggData.classInfo[className];
        if (!classInfo) {
            if (!filterFilepath(filters, pathname)) {
                classlessDocs.push(pathname);
            }
        }
        else {
            var linkElems = unist_util_select_1.selectAll('link', tree);
            linkElems.forEach(function (linkElem) {
                var normUrl = normaliseLinkPath(pathname, linkElem.url);
                multiSetAdd(linkRefs, normUrl, pathname);
            });
        }
        var imageElems = unist_util_select_1.selectAll('image', tree);
        imageElems.forEach(function (imageElem) {
            var normUrl = normaliseLinkPath(pathname, imageElem.url);
            multiSetAdd(imageRefs, normUrl, pathname);
            if (!fs.existsSync(normUrl)) {
                brokenImageRefs[normUrl] = pathname;
            }
        });
    });
    classlessDocs.forEach(function (docPath) {

unist-util-select

unist utility to select nodes with CSS-like selectors

MIT
Latest version published 6 months ago

Package Health Score

73 / 100
Full package analysis