Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the MDN API rather than using DOMParser #162

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Working out index issue
  • Loading branch information
nathancyam committed Jul 4, 2020
commit 6ef1c067177d4806502d396aa083dc2cf1e5a46d
102 changes: 0 additions & 102 deletions src/commands/mdn/__fixtures__/responses.ts

This file was deleted.

47 changes: 47 additions & 0 deletions src/commands/mdn/__snapshots__/api.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`updatedMDNQuery should work 1`] = `
Array [
Array [
Object {
"embed": Object {
"author": null,
"color": 8638706,
"description": "1. [**Document directive** - CSP document directives are used in a Conten...](https://developer.mozilla.orgGlossary/Document_directive)
2. [**document environment** - When the JavaScript global environment is ...](https://developer.mozilla.orgGlossary/document_environment)
3. [**DOM (Document Object Model)** - The DOM (Document Object Model) is ...](https://developer.mozilla.orgGlossary/DOM)
4. [**Archived open Web documentation** - The documentation listed below ...](https://developer.mozilla.orgArchive/Web)
5. [**Document.documentElement** - Document.documentElement returns the E...](https://developer.mozilla.orgWeb/API/Document/documentElement)
6. [**Document.documentURI** - The documentURI read-only property of the ...](https://developer.mozilla.orgWeb/API/Document/documentURI)
7. [**Document.documentURIObject** - The Document.documentURIObject read-...](https://developer.mozilla.orgWeb/API/Document/documentURIObject)
8. [**Document** - The Document interface represents any web page loaded ...](https://developer.mozilla.orgWeb/API/Document)
9. [**Document()** - The Document constructor creates a new Document obje...](https://developer.mozilla.orgWeb/API/Document/Document)
10. [**@document** - The @document CSS at-rule restricts the style rules c...](https://developer.mozilla.orgWeb/CSS/@document)

:bulb: *react with a number (:one:, :two:, ...) to filter your result*
:neutral_face: *react with \`❌\` to delete*
:point_up: *supports \`!mdn\`, \`!github\`, \`!caniuse\`, \`!npm\`, \`!composer\`, \`!bundlephobia\`, and \`!php\`*
:gear: *issues? feature requests? head over to [github](https://github.com/ljosberinn/webdev-support-bot)*",
"fields": Array [],
"footer": Object {
"iconURL": "https://avatars0.githubusercontent.com/u/7565578",
"text": "10 results found",
},
"title": "MDN results for *Search Term*",
"url": "https://developer.mozilla.org/en-US/search?q=Search%20Term",
},
},
],
]
`;

exports[`updatedMDNQuery should work 2`] = `
Array [
Array [
"https://developer.mozilla.org/Glossary/DOM",
Object {
"embed": null,
},
],
]
`;
104 changes: 103 additions & 1 deletion src/commands/mdn/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,111 @@
import { updatedQueryBuilder } from './api';

import { searchResponse } from './__fixtures__/responses';
import useData from '../../utils/useData';
import { getChosenResult } from '../../utils/discordTools';

const searchResponse = {
query: 'document',
locale: 'en-US',
page: 1,
pages: 383,
start: 1,
end: 10,
next:
'https://developer.mozilla.org/api/v1/search/en-US?highlight=false&page=2&q=document',
previous: null,
count: 3823,
filters: [
{
name: 'Topics',
slug: 'topic',
options: [
{
name: 'APIs and DOM',
slug: 'api',
count: 2609,
active: true,
urls: {
active: '/api/v1/search/en-US?highlight=false&q=document&topic=api',
inactive: '/api/v1/search/en-US?highlight=false&q=document',
},
},
],
},
],
documents: [
{
title: 'Document directive',
slug: 'Glossary/Document_directive',
locale: 'en-US',
excerpt:
'CSP document directives are used in a Content-Security-Policy header and govern the properties of a document or worker environment to which a policy applies.',
},
{
title: 'document environment',
slug: 'Glossary/document_environment',
locale: 'en-US',
excerpt:
"When the JavaScript global environment is a window or an iframe, it is called a document environment. A global environment is an environment that doesn't have an outer environment.",
},
{
title: 'DOM (Document Object Model)',
slug: 'Glossary/DOM',
locale: 'en-US',
excerpt:
'The DOM (Document Object Model) is an API that represents and interacts with any HTML or XML document. The DOM is a document model loaded in the browser and representing the document as a node tree, where each node represents part of the document (e.g. an element, text string, or comment).',
},
{
title: 'Archived open Web documentation',
slug: 'Archive/Web',
locale: 'en-US',
excerpt:
'The documentation listed below is archived, obsolete material about open Web topics.',
},
{
title: 'Document.documentElement',
slug: 'Web/API/Document/documentElement',
locale: 'en-US',
excerpt:
'Document.documentElement returns the Element that is the root element of the document (for example, the html element for HTML documents).',
},
{
title: 'Document.documentURI',
slug: 'Web/API/Document/documentURI',
locale: 'en-US',
excerpt:
'The documentURI read-only property of the Document interface returns the document location as a string.',
},
{
title: 'Document.documentURIObject',
slug: 'Web/API/Document/documentURIObject',
locale: 'en-US',
excerpt:
'The Document.documentURIObject read-only property returns an nsIURI object representing the URI of the document.',
},
{
title: 'Document',
slug: 'Web/API/Document',
locale: 'en-US',
excerpt:
"The Document interface represents any web page loaded in the browser and serves as an entry point into the web page's content, which is the DOM tree.",
},
{
title: 'Document()',
slug: 'Web/API/Document/Document',
locale: 'en-US',
excerpt:
"The Document constructor creates a new Document object that is a web page loaded in the browser and serving as an entry point into the page's content.",
},
{
title: '@document',
slug: 'Web/CSS/@document',
locale: 'en-US',
excerpt:
'The @document CSS at-rule restricts the style rules contained within it based on the URL of the document. It is designed primarily for user-defined style sheets, though it can be used on author-defined style sheets, too.',
},
],
};

describe('updatedMDNQuery', () => {
const mockUseData: jest.MockedFunction<typeof useData> = jest.fn();
const mockChoose: jest.MockedFunction<typeof getChosenResult> = jest.fn();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/mdn/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const updatedQueryBuilder = (
return;
}

const editableUrl = buildDirectUrl(provider, `/` + result.slug);
const editableUrl = buildDirectUrl(provider, result.slug);
await attemptEdit(sentMsg, editableUrl, { embed: null });
} catch (error) {
console.error(error);
Expand Down
Empty file added src/commands/mdn/index.test.ts
Empty file.
2 changes: 1 addition & 1 deletion src/utils/urlTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const providers: ProviderMap = {
mdn: {
color: 0x83d0f2,
createTitle: (searchTerm: string) => `MDN results for *${searchTerm}*`,
direct: `https://developer.mozilla.org${TERM}`,
direct: `https://developer.mozilla.org/${TERM}`,
help: '!mdn localStorage',
icon: 'https://avatars0.githubusercontent.com/u/7565578',
search: `https://developer.mozilla.org/en-US/search?q=${SEARCH_TERM}`,
Expand Down