Skip to content

Commit

Permalink
Parse headers (like <h1>) to markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jsh9 committed Sep 14, 2023
1 parent aebd2c8 commit 7edd696
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/minimizedChatGptToMarkdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "ChatGPT to Markdown",
"version": "0.0.6",
"version": "0.0.7",
"description": "A Chrome extension to save ChatGPT Q&A page as markdown",
"permissions": ["activeTab", "scripting"],
"action": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chatgpt-to-markdown",
"version": "0.0.6",
"version": "0.0.7",
"description": "Save ChatGPT Q&A page as markdown",
"scripts": {
"build": "npx webpack --mode production",
Expand Down
17 changes: 17 additions & 0 deletions src/utils/parseNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ function parseNode(node, level) {
if (childNode.nodeType === Node.ELEMENT_NODE) {
const tag = childNode.tagName;

if (['H1', 'H2', 'H3', 'H4', 'H5', 'H6'].includes(tag)) {
nodeMarkdown += parseHeader(childNode, tag);
}
if (['P', 'LI', 'STRONG', 'EM', 'DEL'].includes(tag)) {
nodeMarkdown += parseParagraph(childNode);
}
Expand Down Expand Up @@ -88,6 +91,20 @@ function parseParagraph(node) {
return replaceString(node.outerHTML);
}

function parseHeader(node, tag) {
var headerLevel = parseInt(tag.charAt(tag.length - 1), 10);
var pounds = generatePounds(headerLevel);
return pounds + ' ' + replaceString(node.outerHTML);
}

function generatePounds(count) {
let pounds = '';
for (let i = 0; i < count; i++) {
pounds += '#';
}
return pounds;
}

function parseBlockQuote(node, level) {
var blockQuoteMarkdown = '\n';
const spaces = getSpaces(level);
Expand Down
1 change: 1 addition & 0 deletions tests/utils/parseElements.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ global.Node = {
};

test.each([
['headers_in_answer.html', 'headers_in_answer.md'],
['2023_08_new_format.html', '2023_08_new_format.md'],
['plain_paragraphs.html', 'plain_paragraphs.md'],
['block_quotes_0.html', 'block_quotes_0.md'],
Expand Down
1 change: 1 addition & 0 deletions tests/utils/test_cases/headers_in_answer.html

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions tests/utils/test_cases/headers_in_answer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Question

Hi, please generate a response of a few paragraphs, and separate the paragraphs into several sections, each with a section header? Please make the section headers larger.

## Answer

# Introduction

Hello! I'm here to provide you with a well-structured response, with each section clearly labeled for better organization and readability. Let's dive right in.

# Section 1: The Importance of Effective Communication

Effective communication is a fundamental skill in both personal and professional settings. It plays a pivotal role in conveying ideas, building relationships, and achieving goals. Whether you're engaging in a conversation with a friend, delivering a presentation at work, or writing an email, how you communicate can significantly impact the outcome. Clear and organized communication is essential for avoiding misunderstandings and ensuring that your message is received as intended.

# Section 2: Benefits of Structured Paragraphs

Structured paragraphs are an integral part of effective communication. They help break down complex ideas into manageable chunks, making it easier for readers to follow your thoughts. Here are a few key benefits of using structured paragraphs:

## 2.1 Clarity

Breaking your content into paragraphs allows you to focus on one idea at a time. This clarity helps readers absorb information more easily and reduces the risk of confusion.

## 2.2 Readability

Well-organized paragraphs enhance the overall readability of your text. They provide visual breaks, making the content less daunting and more inviting for readers.

## 2.3 Emphasis

Structured paragraphs allow you to emphasize key points. When a critical concept is presented in its paragraph, it stands out, helping readers grasp the main takeaways more effectively.

# Section 3: Tips for Effective Paragraph Structure

Creating structured paragraphs requires attention to detail. Here are some tips to help you master this essential skill:

## 3.1 Topic Sentences

Start each paragraph with a clear topic sentence that conveys the main idea. This sentence acts as a roadmap for the rest of the paragraph.

## 3.2 Supporting Details

Provide supporting details and evidence within the paragraph to reinforce your main point. These details add depth and credibility to your writing.

## 3.3 Transition Sentences

Use transition sentences to smoothly connect paragraphs. Transitions guide readers through your content, ensuring a logical flow.

# Conclusion

In conclusion, effective communication relies on structured paragraphs to convey ideas clearly and coherently. By breaking down your content into organized sections, you can enhance clarity, readability, and emphasis. Remember to start each paragraph with a strong topic sentence, support your ideas with relevant details, and use transitions to maintain a smooth flow. These practices will not only improve your writing but also make your communication more impactful in various aspects of your life.

0 comments on commit 7edd696

Please sign in to comment.