Skip to content

Commit

Permalink
fix: allow customers to define the slug in the metadata (readmeio#375)
Browse files Browse the repository at this point in the history
* fix: allow customers to define the slug in the metadata

* chore: replacing a real looking category id in a test with a fake one

Co-authored-by: Jon Ursenbach <[email protected]>
  • Loading branch information
mjcuva and erunion authored Oct 5, 2021
1 parent fd383f9 commit 137411d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions __tests__/__fixtures__/slug-docs/new-doc-slug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
category: CATEGORY_ID
title: This is the document title
slug: marc-actually-wrote-a-test
---

Body
34 changes: 34 additions & 0 deletions __tests__/cmds/docs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,40 @@ describe('rdme docs', () => {
});
});
});

describe('slug metadata', () => {
it('should use provided slug', () => {
const slug = 'new-doc-slug';
const doc = frontMatter(fs.readFileSync(path.join(fixturesDir, `/slug-docs/${slug}.md`)));
const hash = hashFileContents(fs.readFileSync(path.join(fixturesDir, `/slug-docs/${slug}.md`)));

const getMock = getNockWithVersionHeader(version)
.get(`/api/v1/docs/${doc.data.slug}`)
.basicAuth({ user: key })
.reply(404, {
error: 'DOC_NOTFOUND',
message: `The doc with the slug '${slug}' couldn't be found`,
suggestion: '...a suggestion to resolve the issue...',
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

const postMock = getNockWithVersionHeader(version)
.post(`/api/v1/docs`, { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash })
.basicAuth({ user: key })
.reply(201, { slug: doc.data.slug, body: doc.content, ...doc.data, lastUpdatedHash: hash });

const versionMock = nock(config.host)
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

return docs.run({ folder: './__tests__/__fixtures__/slug-docs', key, version }).then(() => {
getMock.done();
postMock.done();
versionMock.done();
});
});
});
});

describe('rdme docs:edit', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ exports.run = async function (opts) {
const matter = frontMatter(file);

// Stripping the subdirectories and markdown extension from the filename and lowercasing to get the default slug.
const slug = path.basename(filename).replace(path.extname(filename), '').toLowerCase();
const slug = matter.data.slug || path.basename(filename).replace(path.extname(filename), '').toLowerCase();
const hash = crypto.createHash('sha1').update(file).digest('hex');

return fetch(`${config.host}/api/v1/docs/${slug}`, {
Expand Down

0 comments on commit 137411d

Please sign in to comment.