1
\$\begingroup\$

I was looking to get some reviews of my code, so I can improve my writing. Functionally it works the exact way I want it to based on my patch notes .md file. I'm open to all suggestions for improving the code, especially in making it more performant.

	state = {
		open: false,
		notes: null
	}

	componentDidMount() {
		this.getData();
	}

	getData = () => {
		axios.get(patchnotes_md_urlpath)
			.then((response) => {
				const updates = response.data.replace(/(\r\n|\n|\r)/g, '').split('### ');
				let i;
				let j;
				let notes = [];
				for ( i = 1; i < updates.length; i++ ) {
					let update = updates[i].split('* ');
					const newObj = Object.assign({});
					newObj.version = update[0];
					let list = [];
					for ( j = 1; j < update.length; j++ ) {
						list.push(update[j]);
					}
					newObj.notes = list;
					notes.push(newObj);
				}
				this.setState({
					notes
				});
			}).catch((error) => {
				console.error(error);
			});
	}

### 0.1.2

* Patch notes now implemented when the modal starts.
* Added top lists for organizations

### 0.0.1

* Patch Notes Added
* Modal to include Patch Notes

\$\endgroup\$
  • \$\begingroup\$ Could you edit with an example markdown file you would be using? \$\endgroup\$ – ByteSettlement Jan 2 at 22:00
  • \$\begingroup\$ Added an example .MD file. \$\endgroup\$ – Hamburguesa Jan 3 at 1:25
0
\$\begingroup\$

You got the first step right by removing every white space and line breaks from your string. Next you will have to remove the first occurence of ### by splitting your string and then slicing out the first element.

When done, you will have to map the result of your split. Map will create a new array the exact same size of your input and apply the same function on every attribute of it.

You will then have to split each update by the *. When done, you can get the first value of your array with shift. This function will give out the first element of your array and delete it from the array, only leaving the notes that are going to be put in the notes variable :

const data = '### 0.1.2\n* Patch notes now implemented when the modal starts.\n* Added top lists for organizations\n### 0.0.1\n* Patch Notes Added\n* Modal to include Patch Notes'

const updates = data.replace(/(\r\n|\n|\r)/g, '').split('### ').slice(1).map(update => {
	updateData = update.split('* ')
	return {
		version: updateData.shift(),
		notes: updateData
	}
})

console.log(updates)

You can then put this function in the .then of your axios call

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.