Skip to content

Commit

Permalink
fix: plaintext generation
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Aug 19, 2024
1 parent dc4d78a commit 571c1cf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
9 changes: 6 additions & 3 deletions src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,15 @@ export default async (config = {}) => {
if (Boolean(plaintextConfig) || !isEmpty(plaintextConfig)) {
const posthtmlOptions = get(rendered.config, 'posthtml.options', {})

const plaintext = await generatePlaintext(rendered.html, merge(plaintextConfig, posthtmlOptions))
rendered.html = await handlePlaintextTags(rendered.html, posthtmlOptions)
await writePlaintextFile(plaintext, rendered.config)
await writePlaintextFile(
await generatePlaintext(rendered.html, merge(plaintextConfig, posthtmlOptions)),
rendered.config
)
.catch(error => {
throw new Error(`Error writing plaintext file: ${error}`)
})

rendered.html = await handlePlaintextTags(rendered.html, posthtmlOptions)
}

/**
Expand Down
34 changes: 19 additions & 15 deletions src/generators/plaintext.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,11 @@ export async function writePlaintextFile(plaintext = '', config = {}) {
const plaintextExtension = get(plaintextConfig, 'output.extension', 'txt')

/**
* If `plaintext: true` (either from Front Matter or from config)
* If `plaintext: true` (either from Front Matter or from config),
* output plaintext file in the same location as the HTML file.
*/
if (plaintextConfig === true) {
// If the template has a `permalink` key set in the FM
if (typeof config.permalink === 'string') {
// Output plaintext at the `permalink` path
plaintextOutputPath = config.permalink
} else {
// Output plaintext at the same directory as the HTML file
plaintextOutputPath = get(config, 'build.output.path')
}
plaintextOutputPath = get(config, 'build.output.path')
}

/**
Expand All @@ -179,12 +173,22 @@ export async function writePlaintextFile(plaintext = '', config = {}) {
// No need to handle if it's an object, since we already set it to that initially

/**
* If `plaintextOutputPath` is a file path, output file there
* If the template has a `permalink` key set in the FM, always output plaintext file there
*/
if (typeof config.permalink === 'string') {
plaintextOutputPath = config.permalink
}

/**
* If `plaintextOutputPath` is a file path, output file there.
*
* The file will be output relative to the project root, and the extension
* doesn't matter, it will be replaced with `plaintextExtension`.
*/
if (path.extname(plaintextOutputPath)) {
// Ensure the target directory exists
await lstat(plaintextOutputPath).catch(async () => {
await mkdir(plaintextOutputPath, { recursive: true })
await mkdir(path.dirname(plaintextOutputPath), { recursive: true })
})

// Ensure correct extension is used
Expand All @@ -193,19 +197,19 @@ export async function writePlaintextFile(plaintext = '', config = {}) {
path.basename(plaintextOutputPath, path.extname(plaintextOutputPath)) + '.' + plaintextExtension
)

console.log('plaintextOutputPath', plaintextOutputPath);

return writeFile(plaintextOutputPath, plaintext)
}

/**
* If `plaintextOutputPath` is a directory path, output file there, using the template's name
* If `plaintextOutputPath` is a directory path, output file there using the Template's name.
*
* The file will be output relative to the `build.output.path` directory.
*/
const templateFileName = get(config, 'build.current.path.name')

plaintextOutputPath = path.join(
path.dirname(plaintextOutputPath),
get(config, 'build.current.path.dir'),
plaintextOutputPath,
templateFileName + '.' + plaintextExtension
)

Expand Down
29 changes: 27 additions & 2 deletions test/plaintext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ describe.concurrent('Plaintext', () => {
expect(result).toBe(expected)
})

test.skip('Outputs plaintext files', async ctx => {
test('Outputs plaintext files', async ctx => {
/**
* `plaintext` as a file path
*/
const withOptions = await writePlaintextFile('test', {
plaintext: `${ctx.folder}/plaintext.txt`
})
Expand All @@ -93,9 +96,31 @@ describe.concurrent('Plaintext', () => {
expect(withOptionsFiles).toContain('plaintext.txt')
expect(withOptionsFileContents).toBe('test')

/**
* `plaintext` as a directory path
*/
const withDirPath = await writePlaintextFile('test', {
build: {
current: {
path: {
name: 'plaintext'
}
}
},
plaintext: `${ctx.folder}/txt`
})

const withDirPathFiles = await readdir(`${ctx.folder}/txt`)
const withDirPathFileContents = await readFile(`${ctx.folder}/txt/plaintext.txt`, 'utf8')

// Successful file write fulfills the promise with `undefined`
expect(withDirPath).toBe(undefined)
expect(withDirPathFiles).toContain('plaintext.txt')
expect(withDirPathFileContents).toBe('test')

const withPermalink = await writePlaintextFile('with permalink', {
plaintext: true ,
permalink: `${ctx.folder}/plaintext2.txt`
permalink: `${ctx.folder}/plaintext2.html`
})

const withPermalinkFiles = await readdir(ctx.folder)
Expand Down

0 comments on commit 571c1cf

Please sign in to comment.