asciidoctor / asciidoctor.js

:scroll: A JavaScript port of Asciidoctor, a modern implementation of AsciiDoc
https://asciidoctor.org
MIT License
729 stars 135 forks source link

Help with Reading Content of Included Files in Asciidoctor Include Processor Extension #1714

Closed Trojan13 closed 9 months ago

Trojan13 commented 10 months ago

Environment:

I am currently working on an Asciidoctor include processor extension. The goal is to include gherkin feature files and customize their AsciiDoc markup. However, I am encountering challenges in correctly reading the content of these included files.

Current Implementation: Here's the relevant part of my current code:

const fs = require('fs');
const path = require('path');
const {generateMessages, makeSourceEnvelope, messages} = require(
    '@cucumber/messages');

module.exports.register = function (registry) {
  registry.includeProcessor(function () {
    this.$option('position', '>>')
    this.handles(function (target) {
      return target.endsWith('.feature');
    });

    this.process(function (doc, reader, target, attrs) {
      processFeatureFile(doc, reader, target, attrs);
    });
  });
};

async function processFeatureFile(doc, reader, target, attrs) {
  try {
    console.log(doc.attributes['$$smap']['page-origin-branch']);
    console.log(doc.attributes['$$smap']['page-origin-refname']);
    console.log(doc.attributes['$$smap']['page-origin-worktree']);

    const featureFilePath = doc.attributes['$$smap']['page-origin-worktree'] ? path.resolve(
        doc.attributes['$$smap']['page-origin-worktree'], target) : target;
    console.log(featureFilePath);

    const featureFileContent = fs.readFileSync(featureFilePath, 'utf8');

    let processedContent;
    if (target.endsWith('.feature')) {
      processedContent = convertFeatureToAsciiDoc(featureFileContent);
    }

    reader.pushInclude(processedContent, doc.attr('docfile'), target,
        startLineNum, attrs);
  } catch (error) {
    console.error('Error processing file:', error);
  }
}

In this code, I am trying to read the content of a .feature file based on the doc.attributes['$$smap'] values. The content is then processed and pushed back into the reader.

How should the file paths be correctly resolved in a scenario where the files are in different directories or modules?

Any assistance, tips, or examples would be greatly appreciated. Thank you!

ggrossetie commented 9 months ago

Hey!

You should ask on the community chat: https://chat.asciidoctor.org. The issue tracker is for verified issues and formal proposals.